php登录不工作

标签 php mysql error-handling

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

编号 用户名 密码 作用

管理员的角色编号为 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
}
?>

当管理员登录时,它工作正常并被重定向到 admin.php 而当普通用户登录时,什么也没有发生,页面只是被刷新。我做错了什么?

最佳答案

您在 role=2 的第一个 if block 中拼错了用户名

$_SESSION['用户名'] = true;

关于php登录不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175332/

相关文章:

ios - Objective c iPhone 重定向到未捕获异常的错误页面

php - 用php获取指定行的所有项目的总值(value)

php - 我不确定我的 JSON 格式是否正确以访问它

php - 为什么我的变量会发生这种情况?

php - 跟踪投票并只允许每个成员投一票

mysql - 在 solr import 中关闭 ResultSet 后不允许操作

python - 查找列表的最大值/最小值时管理空列表/无效输入(Python)

php - 如何使用 mysql 和 laravel 制作 udemy 计时器

mysql - 在 Jenkins 中使用 docker exec 运行 October artisan 命令时连接被拒绝

windows - 如何使用RpcRaiseException()进行良好的错误处理?