我的错误在哪里,我的代码不会回应某些部分?

时间:2018-04-30 15:05:21

标签: php login

我在哪里错误地让我的代码显示错误消息,例如“您没有填写所有字段”或“用户名/密码被删除”? 我有登录工作,一切都很好,现在,我在标题栏中显示错误,但我也希望它显示在表单上。

<?php

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

    include_once 'dbh.php';

    $first = mysqli_real_escape_string($conn, $_POST['first']);
    $last = mysqli_real_escape_string($conn, $_POST['last']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $uid = mysqli_real_escape_string($conn, $_POST['uid']);
    $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

    //Error handlers
    //Check for empty fields
    if(empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
        header("Location: ../signup.php?signup=empty");
        echo "you did not fill in all the fields";
        exit();
    } else {
        //Check if input char are valid
        if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
            header("Location: ../signup.php?signup=invalid");
            exit();
        } else {
            //Check if email is valid
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                header("Location: ../signup.php?signup=email");
                exit();
            } else {
                $sql = "SELECT * FROM users WHERE user_uid= '$uid'";
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);

                if ($resultCheck > 0) {
                    header("Location: ../signup.php?signup=usertaken");
                    echo "the username you have entered is taken";
                    exit();
                } else {
                    //Hashing the password
                    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
                    //Insert the user into the database
                    $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
                    mysqli_query($conn, $sql);
                    header("Location: ../signup.php?signup=success");
                    exit();
                }
            }
        }
    }


} else {
    header("Location: ../signup.php");
    exit();
}

1 个答案:

答案 0 :(得分:1)

这是因为您首先重定向页面然后显示没有意义的消息。如果您想显示消息,可以使用这种方式:

if(empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {

    $msg = "you did not fill in all the fields";
    header("Location: ../signup.php?signup=empty&errmsg=$msg");
    exit();
}

在signup.php页面上使用类似

的内容
if($errmsg)
  echo $errmsg;