如果输入了错误的电子邮件和密码,则不显示错误消息


Error message not showing if the wrong email and password is entered

我有一个登录系统,其中有多个成员类型。我需要这段代码在找不到已输入的电子邮件和密码时显示错误消息。

换句话说,当输入错误的登录详细信息时,我需要它来显示错误消息。目前,当输入错误的详细信息时,页面会刷新。

        $check_user = mysqli_num_rows($user_query); // checking to see if there is infact a user which those credentials in the DB
            if ($check_user==1){
            while ($row = mysqli_fetch_array($user_query)){
                    $id = $row['user_id'];
                    $user_type = $row['account'];
                }
                $_SESSION["user_login"] = $login_user;
                // check the user type and redirect according to it
                if($user_type == "Student"){
                 $student_page = "profile_student.php";
                 header( "Location:{$student_page}" );
               } elseif ($user_type == "Landlord"){
                  $landlord_page = "landlord_profile.php";
                  header( "Location:{$landlord_page}" );
               } elseif ($user_type == "Administrator"){
                   $admin_page = "admin_profile.php";
                   header( "Location:{$admin_page}" );
               }else {
                   $refresh_page = "sign_up.php";
               }
                 header( "Location:{$refresh_page}" ); // refresh page
               exit;
            }else {
                echo "<div class='wrong_login'>
                            <p> Email or password is incorrect, please try again. </p>
                    </div>";

            }
    }
        ?>

在标头之后,必须使用"exit()";否则代码执行将继续。

代码执行继续到最后一个重定向标头,这就是接下来的内容。

已修复,代码更易于阅读:

           if($user_type == "Student"){
               $refresh_page = "profile_student.php";
           } elseif ($user_type == "Landlord"){
              $refresh_page = "landlord_profile.php";
           } elseif ($user_type == "Administrator"){
               $refresh_page = "admin_profile.php";
           }else {
               $refresh_page = "sign_up.php";
           }
           header( "Location:{$refresh_page}" ); // refresh page
           exit;

或做

        if($user_type == "Student"){
             $student_page = "profile_student.php";
             header( "Location:{$student_page}" );
             exit();
        } elseif ($user_type == "Landlord"){
              $landlord_page = "landlord_profile.php";
              header( "Location:{$landlord_page}" );
              exit();
        } elseif ($user_type == "Administrator"){
              ...