I have a problem with my login script, everything woks 100% no errors, but the stmt bind result is always returning 0 and in the database it is 1. so on the dashboard i cant load the users data, because i get the users information based on the session var. Here is the processing script.
<?php
if (isset($_POST['btn_login'])) {
if ($_POST['login_username'] == '') {
$_SESSION['login_error'] = 'Please fill in the username field.';
redirect('login/?error'); // these are custom functions created on a separate file
}
if ($_POST['login_password'] == '') {
$_SESSION['login_error'] = 'Please fill in the password field.';
redirect('login/?error'); // these are custom functions created on a separate file
}
$username = (string) $_POST['login_username'];
$password = (string) hash('sha512', $_POST['login_password']);
$stmt = $mysqli->prepare("SELECT user_id FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
$stmt->bind_result($id);
$_SESSION['id'] = (int) $id;
$_SESSION['login_fingerprint'] = fingerprint(); // fingerprint is a custom function created on a separate file
$stmt->close();
session_regenerate(); // these are custom functions created on a separate file
redirect('dashboard'); // these are custom functions created on a separate file
} else {
$stmt->close();
$_SESSION['login_error'] = 'Wrong username/password entered, please check to see if you entered them correctly.';
redirect('login/?error');
}
}
?>
here is the database structure. It is a simple database structure.
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`username` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(250) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
My goal is to get it to return the user_id and set it to the session var.
Here is a link to the full script