【发布时间】:2018-12-02 07:21:32
【问题描述】:
这是表格:
<form>
<div class="form-group ">
<label for="exampleInputFirstName">First Name</label>
<input type="text" class="form-control" id="exampleInputFirstName" name="FirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="exampleInputLastName">Last Name</label>
<input type="text" class="form-control" id="exampleInputLastName" name="LastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="Email" aria-describedby="emailHelp" placeholder="Enter Email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword">Password</label>
<input type="password" class="form-control" id="exampleInputPassword" name="Password" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Confirm Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="confirmPassword" placeholder="Confirm Password">
</div>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
这是 php:
<?php
$link = mysqli_connect("shareddb-i.hosting.stackcp.net", "LoginCredentials-3337b6db", "subrat410", "LoginCredentials-3337b6db");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
if(isset($_POST['submit'])) {
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$confirmPassword = $_POST['confirmPassword'];
}
$query ="INSERT INTO `Login-Credentials`( `FirstName`, `LastName`, `Email`, `Password`, `ConfirmPassword`) VALUES (' $FirstName ',' $LastName ',' $Email ',' $Password ',' $confirmPassword ')";
// $query = "UPDATE `LoginCredentials` SET password = 'uedjUFH7^%' WHERE email = 'robpercival80@gmail.com' LIMIT 1";
mysqli_query($link, $query);
?>
每次我填写表单并提交时,都会在我的数据库中创建另一个空行。任何帮助将不胜感激。我已经实现了 php.ini 文件并且它没有显示任何类型的错误。此外,在查看页面源代码时,我在声明 $FirstName=$_POST('FirstName') 的 php 代码中看到未定义索引错误。谢谢
【问题讨论】:
-
检查您的 $_POST 变量是否已设置且不为空,请按照以下方式执行操作:
$FirstName = (isset($_POST['FirstName']) && !empty($_POST['FirstName'])) ? $_POST['FirstName'] : false;仅在(插入 if 条件)您没有收到任何“假”值时运行查询。 -
您好先生,我已经应用了上述代码。现在至少它没有在我的数据库中插入空行。我的问题是我在同一个文件 index.php 中编写了 php 和 html 代码,现在我面临无法将表单数据发送到 php 变量的情况。请帮忙