Kevin K
Kevin K

Reputation: 113

Mysql check if email exists in database, keeps returning 0

Okay, heres an update. $userexists stays at 0. Even though the user DOES exist in the database. It should tick to 1 because the user exists.

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
if ($email === ''){
    unset($email);
}
   $nemail = test_input($email);


$userexists=0;
$test = <<<SQL
SELECT email FROM `Members` WHERE email='$nemail'
SQL;
if(!$result = $mysqli->query($test)){
    die('There was an error running the query');
}
while($row = $result->fetch_assoc()){
    $nemail = $row['email'];
    $userexists==1;
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

Upvotes: 1

Views: 65

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

This part of your code $userexists==1;

Remove an equal sign. we're not comparing here, you need to "assign".

$userexists=1;

References:

Plus, the test_input() function you're using isn't the best to test against an SQL injection.

Use a prepared statement, please:

Upvotes: 1

Related Questions