我试图将我的php代码连接到我的数据库,一旦我输入并以html形式提交用户名,电子邮件地址和密码之类的内容。它们将被发送到我的数据库并显示在特定的列中,并且在我的页面上将显示诸如“新记录已成功创建”之类的文本。但是现在,发生了错误。
以下是我的php代码
<?php
$visitorname = $emailaddress = $visitorpassword = $confirmpassword = "";
$servername = "localhost";
$username = "username";
$password = "password";
$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($password, 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, $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";
}
?>
以下是我的网页上显示的错误输出:
已成功连接错误将INSERT INTO注册用户(ID,用户名,电子邮件地址,哈希密码)值(0,Josh5577,josh1998@hotmail.com,$ 2y $ 10 $ RMasEdVOskmcXbmfchLeBeUzLa5l38jXFaCQN7vMEhR8A0mU / iiC6) 未选择数据库
答案 0 :(得分:0)
请使用以下代码进行连接
\w*:[^And]*
答案 1 :(得分:0)
您可以尝试更改
// Create connection
$conn = new mysqli($servername, $dbname);
到
// Create connection
$conn = new mysqli($servername, $username, $password);
mysqli_select_db($conn, $dbname);