首页 > 解决方案 > 用php制作登录脚本的问题

问题描述

我需要一些有关我网站的登录脚本的帮助。我只收到“nouser”错误,但我知道我有一个帐户并且它是正确的登录名。就像它跳过了脚本的登录部分。我检查了大括号,我认为我是对的。好像从数据库中提取有问题?

任何人都可以看到任何错误或可以给我如何改进它的提示吗?

if (isset($_POST['login-submit'])) {

        require "dbh.i.php";

        $mailuid = $_POST['mailuid'];
        $password = $_POST['pwd'];

        if (empty($mailuid) && empty($password)) {
            header("Location: ../index.php?error=emptyfields");
            exit();
        } else {
            $sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers;";
            $stmt = mysqli_stmt_init($connect);

            if (!mysqli_stmt_prepare($stmt, $sql)) {
                header("Location: ../index.php?error=sqlerror");
                exit();
            } else {
                mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);

                if ($row = mysqli_fetch_assoc($result)) {
                    $pwdCheck = password_verify($password, $row['pwdUsers']);

                    if ($pwdCheck == false) {
                        header("Location: ../index.php?error=wrongpwd");
                        exit();
                    } else if ($pwdCheck == true) {
                        session_start();
                        $_SESSION[userId] = $row['idUsers'];
                        $_SESSION[userUid] = $row['uidUsers'];
                        header("Location; ../index.php?login=success");
                        exit();
                    } else {
                        header("Location: ../index.php?error=wrongpwd");
                        exit();
                    }
                } else {
                    header("Location: ../index.php?error=nouser");
                    exit();
                }
            }
        }
    } else {
        header("Location: ../index.php?signup");
        exit(); 
    }

标签: phpmysqlmysqliprepared-statement

解决方案


我认为您的这部分代码是错误的:

$sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers;";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../index.php?error=sqlerror");
    exit();
} else {
    mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);

您的查询中只有一个占位符要替换:SELECT * FROM users WHERE uidUsers=? OR emailUsers;,但您将其替换了两次: mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);

尝试将您的查询更改为:

SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;

推荐阅读