我一直都在谷歌,并一直试图找出为什么这种情况不断发生。我知道在这个网站上已经提出了类似的问题,但他们的回答对我来说没有运气。它确实连接到数据库,但没有显示记录(每列的空记录)。 php代码有额外的注释部分供测试。任何帮助表示赞赏。
这是HTML表单
<form action="../controller/register1.php" method="POST" >
<fieldset>
First Name <input class="inputs" type="text" name="firstname" placeholder="Enter your firstname" required>
<br>
Last Name <input class="inputs" type="text" name="lastname" placeholder="Enter your lastname" required>
<br>
User Name <input class="inputs" type="text" name="username" placeholder="Enter your username" required>
<br>
Password <input class="inputs" type="password" name="password" required>
<br>
Email <input class="inputs" type="text" name="email" >
<br>
<input class="inputs" type="submit" name="submit" value="Register" />
</fieldset>
这是PHP
<?php
$connection = mysqli_connect("localhost","charcoun_donaldh","Rusty1992","charcoun_phplogin","3306") or die("Could not connect!");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$firstname = stripslashes($firstname);
$firstname = mysqli_real_escape_string($connection, $_POST['$firstname']);
//$lastname = stripslashes($lastname);
$lastname = mysqli_real_escape_string($connection, $_POST['$lastname']);
//$username = stripslashes($username);
$username = mysqli_real_escape_string($connection, $_POST['$username']);
//$password = stripslashes($password);
$password = mysqli_real_escape_string($connection, $_POST['$password']);
//$email = stripslashes($email);
$email = mysqli_real_escape_string($connection, $_POST['$email']);
$sql = "INSERT INTO users (firstname, lastname, username, password, email)
VALUES ('$firstname','$lastname','$username','$password','$email')"; //Use ".md5($password)." for encrypted
//$result = mysqli_query($connection, $sql);
if(!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_error($connection));
}
else{
echo "<br/><h3>You have been registered successfully. Click here to return <a href='../view/index.php'>Home.</a></h3>";
}
&GT;
答案 0 :(得分:0)
检索POST变量时删除$
。例如,将$_POST['$firstname']
更改为$_POST['firstname']
。此外,如果您尝试使用$_POST['firstname']
,则应该获得
未定义的索引:$ firstname
错误。如果您没有收到错误,请检查您是否有display errors turned on。