0

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

https://github.com/WhiteNicholas/php_login_system

CodeNow
  • 34
  • 5
  • Don't you need to do a `$stmt->fetch();` after `$stmt->bind_result($id);`? http://stackoverflow.com/questions/17095360/whats-the-difference-between-the-mysqli-functions-bind-result-store-result-and http://stackoverflow.com/questions/22439875/mysqli-bind-result-is-returning-null – Sean Oct 28 '16 at 05:32
  • let me try adding it – CodeNow Oct 28 '16 at 05:33
  • im stupid, that fixed the issue, your amazing. Thanks – CodeNow Oct 28 '16 at 05:35
  • new to stmt prepared statements :P – CodeNow Oct 28 '16 at 05:36

0 Answers0