stmt绑定结果总是返回0

时间:2016-10-28 05:17:20

标签: php mysql sql

我的登录脚本有问题,一切都没有错误,但是stmt绑定结果总是返回0而在数据库中它是1.所以在仪表板上我无法加载用户数据,因为我得到了基于会话var的用户信息。这是处理脚本。

<?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');
  }
}
?>

这是数据库结构。这是一个简单的数据库结构。

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;

我的目标是让它返回user_id并将其设置为session var。

以下是完整脚本的链接

https://github.com/WhiteNicholas/php_login_system

0 个答案:

没有答案