这是表格:
<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代码中看到未定义索引错误。谢谢
答案 0 :(得分:1)
Lovelace是正确的,也许您的$ _POST变量仍未设置。 您的表单标签没有方法或操作属性
<form actions="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
//the rest of your code is fine
</form>
答案 1 :(得分:1)
“ <form>
元素定义了如何发送数据。其所有属性旨在让您配置当用户单击“提交”按钮时要发送的请求。最重要的两个属性是action
和method
。” see more。
您的HTML代码可能类似于:
<form actions=”[target url]” method=“POST”>
<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>
答案 2 :(得分:0)
您的表单应定义一个操作和一个方法,它可能是使用GET而不是POST:
<form action="your/php/file.php" method="post">
请参阅此处以了解表单默认为GET请求的信息(如果使用GET请求路由,PHP $ _GET变量将非常有用):What is the default form HTTP method?
在您的php中,通常使用$ _REQUEST [“ propertyName”]比使用$ _POST [“ propertyName”]或$ _GET [“ propertyName”]总是更好,因为它可以使用任一方法类型滚动
此外,您可能应该将查询放入if语句中,以防止出现空行。
最后,您可以尝试以下php行以查看发生了什么事情:
var_dump($_REQUEST);
var_dump($_GET);
var_dump($_POST);
让我知道您的发现,我们将继续帮助您解决问题