php登录无法正常工作

Jah*_*hed 0 php mysql error-handling

我有一个包含以下字段的mysql表:

id用户名密码角色

管理员的角色编号为1,普通用户的角色编号为2

    <?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['username']) && isset($_POST['password'])) {
    include 'library/connect.php';

    $username   = $_POST['username'];
    $password = $_POST['password'];

    // check if the user id and password combination exist in database
    $sql = "SELECT role FROM user WHERE username = '$username' AND password = '$password'";

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
    $row = mysql_fetch_array($result);

    if (mysql_num_rows($result) == 1 AND $row['role']==2) {
        // the user id and password match, 
        // set the session
        $_SESSION['userame'] = true;
        // after login we move to the main page
        header('Location: login_success.php');
        exit;
    }
    elseif (mysql_num_rows($result) == 1 AND $row['role']== 1) {
        // the user id and password match, 
        // set the session
        $_SESSION['admin'] = true;
        $_SESSION['username'] = true;
        // after login we move to the main page
        header('Location: admin.php');
        exit;
    }
else {
        $errorMessage = 'Sorry, wrong user id / password <a href="login.html">Go Back</a>';
    }

    include 'library/closedb.php';
}
?>



<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
Run Code Online (Sandbox Code Playgroud)

当管理员登录时,它工作正常并被重定向到admin.php,而当普通用户登录时,根本没有任何事情发生,页面才会刷新.我究竟做错了什么?

Joh*_*aig 6

你在第一个if区块拼错了用户名role=2

$_SESSION['userame'] = true;