Home » Php » php – multiple if else statements return a white screen

php – multiple if else statements return a white screen

Posted by: admin February 25, 2020 Leave a comment

Questions:

So, I’m currently working on a log in page for my website and I have two different users for my website but both of them are from two different tables admin_table and partner_table. I find it difficult to chcek on two different tables if the username exists but I got it to work and now, whenever I type a wrong combination of username and password, it only shows a white screen. I tried adding more else’ leading back to index.php with an error but It still won’t work. Sorry I’m new to php.

<?php
    session_start();

    if (isset($_POST['submit'])) {
    include_once 'connect.php';

    $uid = mysqli_real_escape_string($conn, $_POST['username']);
    $pwd = mysqli_real_escape_string($conn, $_POST['password']);

        if (empty($uid) || empty($pwd)) {
            header("Location:../index.php?emptyfield");
            exit();
        } else {
            $sql = "SELECT * FROM admin_table WHERE a_username=?";
            $sql2 = "SELECT * FROM partner_table WHERE p_username=?";
            $stmt = mysqli_stmt_init($conn);
            if (!mysqli_stmt_prepare($stmt,$sql)) {
                header("Location:../index.php?error=sqlerror");
                exit();
            } 
            else {
                mysqli_stmt_bind_param($stmt, "s", $uid);
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                if ($row = mysqli_fetch_assoc($result)) {
                    $pwdCheck = password_verify($pwd, $row['a_password']);
                    if ($pwdCheck == NULL) {
                        echo "<script type='text/javascript'> document.location = '../index.php?invalidpwduid'; </script>";
                        exit();
                    }
                    else if ($pwdCheck = 'true') {
                            $_SESSION['a_id'] = $row['a_id'];
                        echo "<script type='text/javascript'> document.location= '../pages/admin/admin-login.php?login=success'; </script>";
                        }   
                    else {
                        header("Location../index.php?=invalidpwduid");
                        exit();                     
                    }
                } else if (mysqli_stmt_prepare($stmt,$sql2)) {
                        mysqli_stmt_bind_param($stmt, "s", $uid);
                        mysqli_stmt_execute($stmt);
                        $result = mysqli_stmt_get_result($stmt);
                        if ($row = mysqli_fetch_assoc($result)) {
                            $pwdCheck = password_verify($pwd, $row['p_password']);
                            if ($pwdCheck == NULL) {
                                echo "<script type='text/javascript'> document.location = '../index.php?invalidpwduid'; </script>";
                                exit();
                            }
                            else if ($pwdCheck = 'true') {
                                    $_SESSION['p_id'] = $row['p_id'];
                                echo "<script type='text/javascript'> document.location= '../pages/partner/partner-login.php?login=success'; </script>";
                                }   
                            else {
                                header("Location../index.php?=invalidpwduid");
                                exit();                     
                            }
                } else {
                    header("Location../index.php?=invalidpwduid");
                    exit();
                }
            }
                else {
                    header("Location:../index.php?error=nouser");
                    exit();
                }
            }
        }
} 
    else {
        header("Location:../index.php");
        exit();
    }
?>
How to&Answers: