Check if email is registered in the BD

Asked

Viewed 433 times

3

I’m doing a search in my BD to verify the existence of email, avoiding the registration of the same again, but I’m having problem in return, the script below sends and treats the return.

 if (sender.getFieldName() == 'Email') {
 if (sender.getValue()) {

     var emailExist = false;

     $.ajax({
         type: 'GET',
         url: 'AjaxBasedValidation.php',
         data: {
             checkEmail: sender.getValue()
         },
         async: false, 
         dataType: 'json',           
         success: function(dataResult) {
             emailExist = dataResult;
         }
     });

     editors['Email']
         .setState(emailExist ? 'warning' : 'success')
         .setHint(emailExist ? 'E-mail ' + sender.getValue() + ' já está cadastrado' : null);
 } else {
     editors['Email']
         .setState('normal')
         .setHint(null);
 }

}

The code you check in the BD:

    #Recebe o Email Postado
    $emailPostado = $_GET['checkEmail'];

    #Conecta banco de dados 
    $con = mysqli_connect(".", "", "", "");
    $sql = mysqli_query($con, "SELECT * FROM `usuarios` WHERE `Email` = '{$emailPostado}'") or print mysql_error();

    if($rcQuery == true){
        die('{"dataResult" : 0"}'); 
    } else {
        die('{"dataResult" : 1"}');
    }

All emails I inform the script is accusing that are already registered.

2 answers

2


Count the number of rows returned by the query with mysqli_num_rows(). See that in if the comparison is made with a variable that does not exist in the question code $rcQuery

$result = mysqli_query($con, "SELECT * FROM `usuarios` WHERE `Email` = '{$emailPostado}'");
$retorno['dataResult'] = ($result) ? mysqli_num_rows($result) : 0;
echo json_encode($retorno);
  • Hello @rray, I edited the question after your reply, thanks for the tip, I will test.

  • @adventistapr remember to convert the return of ajax into json, var retorno = JSON.parse(dataResult), then you can use. console.log(retorno.dataRresult);

  • Hello @rray, could you give me an example of how to convert the return of ajax to json? I did it the way you gave me, but I don’t know if I did it right. I appreciate it.

  • @adventistapr what happened:? gave error?

  • Hello @rray, still accuses that all e-mail are already registered

  • @adventistapr in javascript if you do console.log(dataResult); shows what?

  • Hello @rray, thanks for the help, I redid the search and debugged the script, now everything is correct. Was missing in the return php, thanks for the hints.

  • @adventistapr tranquil ;) q good q hit the code

Show 3 more comments

1

I know this question has already been answered, but I would like to leave a more secure solution in the case of sql statements aimed at string queries:

In the case of your select it would be safer to run the email queries as follows:

$result = mysqli_query($con, "SELECT * FROM `usuarios` WHERE `Email` LIKE \"%".$emailPostado."%\"");

Another thing, try to study about the class PDO, because these php functions mysql_* and mysqli_* are obsolete and no longer exist after php version 7.

  • Great tip @Rafael Andrade, thanks so much.

  • Thank you @adventistapr, nice to help you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.