-1

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\X$AMIT\login.php on line 13

Line 13 is : if (password_verify($password, $row['password'])) {

Error when I put a data that does not exist in the database.

<?php
    session_start();
    include('includes/connection.php');

    if (isset($_POST['login'])) {
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $password = mysqli_real_escape_string($conn, $_POST['password']);

        $query = "SELECT password, name, status FROM users WHERE username = '$username'";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_assoc($result);

        if (password_verify($password, $row['password'])) {
            $_SESSION['logged_in'] = true;
            
            if ($row['status'] == 'U') {
                $_SESSION['check_status'] = 'U';
                $_SESSION['name'] = $row['name'];
                $_SESSION['success_login'] = "login successfully";
                header('refresh:2; index.php');
            }
            else if ($row['status'] == 'A') {
                $_SESSION['check_status'] = 'A';
                $_SESSION['name'] = $row['name'];
                $_SESSION['success_login'] = "login successfully";
                header('refresh:2; index.php');
            }
        }
        else {
            $_SESSION['err_login'] = "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง";
            #header('refresh:2; login.php');
        }
    }
?>

1 Answers1

0

What you try to do will not work.

After:

 $row = mysqli_fetch_assoc($result);

You have to check if $row has any value. If it has no value the input of password and user was wrong so represent the the usre the inlog form.

Henry
  • 1,242
  • 1
  • 12
  • 10