0

I have this code:

$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

if (mysqli_num_rows($result) == 1) {
    $errorEmail = 'This email is registered m8';
    $mainError = true;
}

So the problem is that it always says that the email is registered even if it is not for the email check (email check is after name check). I tried changing variable names but it doesnt work.

$checkDB = "SELECT user_name FROM users WHERE user_name='$name'";
    $result = mysqli_query($conn, $checkDB);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

    if (mysqli_num_rows($result) == 1) {
        $errorName = 'Name registered';
        $mainError = true;
    }
Tolga20
  • 17
  • 7

2 Answers2

0

mysqli_num_rows($result); checks the numbers of rows of found email. And don't put mysqli_fetch_array($result, MYSQLI_ASSOC);. I don't know why but its work for me.

Use this code

$checkDB = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($conn, $checkDB);
$check = mysqli_num_rows($result);

if ( $check == 1) {
    $errorEmail = 'This email is registered m8';
    $mainError = true;
}
Martin
  • 22,212
  • 11
  • 70
  • 132
saqib kifayat
  • 136
  • 2
  • 14
0
  • You should check if it is greater to zero
  • Fetching is not necessary
  • Also in your stituation you should be preparing your statment.

$checkDB = "SELECT email FROM users WHERE email= ?";
if ($result = mysqli_prepare($conn, $checkDB)) {
    mysqli_stmt_bind_param($result, "s", $email);
    mysqli_stmt_execute($result );
    if(mysqli_num_rows($result) > 0){
        $errorName = 'Name registered';
        $mainError = true;
    }
}
mysqli_close($conn);
meda
  • 45,103
  • 14
  • 92
  • 122