尝试将哈希密码插入Phpmyadmin,但出现错误:未知列

时间:2019-03-01 11:19:53

标签: php mysql mysqli

试图将哈希密码插入Phpmyadmin,但我得到了

  

错误:“字段列表”中的未知列'$ 2y $ 10 $ fMTEqdQnl3E3EQrq0t2TcelmNGhghIQz0kmXPeFyWKq0BgH7BHxYm'

<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "usersystem";

$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname); 

$Username = $_POST["usrnm"];
$Password = $_POST["psw"];

$hash = password_hash($Password, PASSWORD_BCRYPT);

$sql = "INSERT INTO users (Username, Password) VALUES ('$Username', $hash)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}



$conn->close();
?>

1 个答案:

答案 0 :(得分:2)

您需要在哈希周围添加单引号

$sql = "INSERT INTO users (Username, Password) VALUES ('$Username', '$hash')";

MySQL会将不带单引号的任何字符串视为保留的关键字/列名或表名。

如果查看错误,则会发现确切的问题:

  

错误:未知列   '$ 2y $ 10 $ fMTEqdQnl3E3EQrq0t2TcelmNGhghIQz0kmXPeFyWKq0BgH7BHxYm'in   “字段列表”

这意味着hash的值将被用作字段名而不是值。