3

因此,当用户登录到控制面板时,我试图存储用户的“排名”,该控制面板根据给定的排名显示不同的选项。我使用与存储和显示用户名相同的方法,该用户名显示在每个页面的顶部并且工作正常。我一生都无法弄清楚为什么它对排名值不起作用,但我知道它不会在会话中保存它。这是不起作用的位;

$username = ($_POST['username']);
$password = hash('sha512', $_POST['password']);

$dbhost = 'mysql:host=██████████████████████;dbname=███████████';
$dbuser = '███████████';
$dbpassword = '████████████████';

try {
$db = new PDO($dbhost, $dbuser, $dbpassword);
$statement = $db->prepare("select password from users where email = :name");
$statement->execute(array(':name' => $username));
$result = $statement->fetch();
$pass = $result[password];
$rank = $result[rank];}

catch(PDOException $e) {echo $e->getMessage();}

if ($password == $pass) {

session_start();
$_SESSION['username'] = $username;
$_SESSION['rank'] = $rank;
header('Location: http://█████████.ca/manage.php');

}
else{
include'../../includes/head.inc';
echo '<h1>Incorrect username or password.</h1>';
include'../../includes/footer.inc';
}

我对整个 PDO 也很陌生,因此我验证密码的方法非常粗略。

4

1 回答 1

3

并不是说它不保存排名 - 显然您没有从查询中检索它。您只要求输入密码。用户名来自其他地方:

$db = new PDO($dbhost, $dbuser, $dbpassword);
$statement = $db->prepare("select password from users where email = :name");

// Should be select password, rank

$statement->execute(array(':name' => $username));
$result = $statement->fetch();
$pass = $result[password];
$rank = $result[rank];}
于 2012-06-28T15:11:05.443 回答