0

Trying to insert a hashed password into Phpmyadmin, but i get an

Error: Unknown column '$2y$10$fMTEqdQnl3E3EQrq0t2TcelmNGhghIQz0kmXPeFyWKq0BgH7BHxYm' in 'field list'

<?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();
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62

1 Answers1

2

You need to add single quote around hash

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

MySQL considers any string without single quotes as reserved keyword/column name or table name.

If you look at the error, you will find the exact issue:

Error: Unknown column '$2y$10$fMTEqdQnl3E3EQrq0t2TcelmNGhghIQz0kmXPeFyWKq0BgH7BHxYm' in 'field list'

It means value of hash is getting served as field name rather than value.

Pupil
  • 23,834
  • 6
  • 44
  • 66