For some reason when I use mysqli prepared statements to select values from a database, they don't seem to work. The attached code is supposed to scan a database for a username and a password, and in case they match, display a message.
I've tried many solutions from Stack Overflow, but none seem to work for me. For some reason the $hash
in the if (password_verify($password, $hash))
doesn't recieve the value it should from the database
here is the code:
if(isset($_POST['submit'])) {
$dbc = mysqli_connect('localhost', 'root', '', 'users') or die('Error connecting to MySQL server.' . mysqli_connect_error());
if ($dbc) {
if (isset($_POST["submit"])) {
$user = $_POST["usern"];
$password = $_POST["pass"];
$query = "SELECT username, password FROM user_db WHERE username = ? AND password = ?";
$stmt = mysqli_stmt_init($dbc);
if (mysqli_stmt_prepare($stmt, $query)) {
mysqli_stmt_bind_param($stmt, 'ss', $user,$hash);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $username, $hash);
mysqli_stmt_fetch($stmt);
if (password_verify($password, $hash)) {
echo "Login successful";
} else {
echo "Wrong username or password";
}
mysqli_stmt_close($stmt);
}
}
}
mysqli_close($dbc); }
Edit: The issue was solved by removing AND password = ?
from the statement and the $hash
from the mysqli_stmt_bind_param()
function.
The answers from @aynber and @Dharman helped a lot, along with this answer