我一直在尝试确保代码中的所有内容都是正确的,但是此错误仍然存在。
我有一个名为“ mydb”的数据库和一个名为“ registereduser”的表。我当前的目标是在填写html表单后提交数据。数据提交成功后,将显示“成功创建新记录”之类的文本,这意味着提交的数据应该在我的数据库中并显示在相应的列中。
以下是我的php代码。
<?php
$visitorname = $emailaddress = $visitorpassword = $confirmpassword = "";
$servername = "localhost";
$username = //my username for myphpadmin goes here";
$password = //my password for myphpadmin goes here";
$dbname = "mydb";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$visitorname = input($_POST["visitorname"]);
$emailaddress = input($_POST["emailaddress"]);
$visitorpassword = input($_POST["visitorpassword"]);
$confirmpassword = input($_POST["confirmpassword"]);
$hashed_password = password_hash($visitorpassword, PASSWORD_DEFAULT);
}
function input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if($visitorpassword == $confirmpassword){
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_select_db($conn, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$id = mysqli_insert_id($conn);
$sql = "INSERT INTO registereduser (id, username, emailaddress, hashedpassword) VALUES ($id, $visitorname, $emailaddress, $hashed_password)";
if ($conn->query($sql) === TRUE){
echo "New record created successfully";
}else{
echo "Errors " . $sql . "<br>" . $conn->error;
}
$conn->close();
}else{
echo "Passwords do not match";
}
?>
在页面上提交html表单后,它返回此错误:
已成功连接错误将INSERT INTO注册的用户(ID,用户名, emailaddress,hashedpassword)VALUES(0,Josh5577, josh1998@hotmail.com, $ 2y $ 10 $ ojTeOyBK6dmfEVEsoy9u6.Yq7n / rHrNs / f6MMXktX6ZCkZFLCTLya) 您的SQL语法错误;检查与您的手册相对应的手册 MariaDB服务器版本,可在'@hotmail.com附近使用正确的语法, $ 2y $ 10 $ ojTeOyBK6dmfEVEsoy9u6.Yq7n / rHrNs / f6MMXktX6ZCkZFLCTLya)' 1