0

I am fetching data from the database like this

while ($row = mysqli_fetch_array($select_user_query)){
    $userId = $row ['id'];
    $check_email = $row ['userEmail'];
    $check_password = $row ['userPassword'];
}

and then I compare if the entered details match the details fetched from the db.

if ($email === $check_email || $hashed_password ===  $check_password) {

   // header ("Location:../dashboard.php");
    echo "success";

} else {

   // header ("Location: ../signup.php");
    echo "failed";
}

but the problem is, if the email address entered doesn't exist in the database, it returns error

Notice: Undefined variable: check_email in C:\xampp2\htdocs\honest\includes\login_process.php on line 37

However if the email exists, then it returns success.

So the questions is, what is the best practice here? Should I set default value to $check_email if it's empty?

Phil
  • 157,677
  • 23
  • 242
  • 245
Rayan Sp
  • 1,002
  • 7
  • 17
  • 29

2 Answers2

1

check_email parameter coming from the query is null, so you need to do a null check before assigning it to another variable, i.e.

$check_email = $row['userEmail'] != null ? $row['userEmail'] : '';
Matt G
  • 1,332
  • 2
  • 13
  • 25
1

Assuming that userEmail is unique, your table is named "user_table" and $conn is your mysqli connection / instance, you can check for a matching row like this...

$stmt = $conn->prepare(
    'SELECT `id` FROM `user_table` WHERE `userEmail` = ? AND `userPassword` = ? LIMIT 1');
$stmt->bind_param('ss', $email, $hashed_password);
$stmt->execute();

$stmt->bind_result($userId);
if ($stmt->fetch()) {
    echo 'success';
    // $userId is also set to the result `id` now
} else {
    echo 'failed';
}
Phil
  • 157,677
  • 23
  • 242
  • 245