-2

I want to check to see if an email already exists in a table or not. I am using bootstrap formValidator library, with remote method, but its not making any proper result. It always shows the same message, whether its wrong or right, dont know whats wrong. remote.php

    <?php
include("dbcontroller");
$dbhandle=new DBcontroller();
header('Content-type: application/json');

include("connect.php");
$sql="select * from members";
$temp=array();
$result=$db_handle->runQuery($sql);
foreach($result as $row)
{
   $temp[]=$row['email'];
}


$valid = true;

if (isset($_POST['email'])) {
    $email = $_POST['email'][0];
    foreach ($temp as $k => $v) {
        if ($email == $v) {
            $valid = false;
            break;
        }
    }
}

echo json_encode(array(
    'valid' => $valid,
));
?>

Form.php

<div class="form-group">
     <label class="col-lg-2 control-label">Email</label>
     <div class="col-lg-8">
          <input type="text" placeholder="Email" id="e_mail" class="form-control" name="email[]" autocomplete="off"/>
     </div>
</div>

Javascript file

'emaill[]': {
                validators: {
                        notEmpty: {
                            message: 'The email address is required and can\'t be empty'
                        },
                        emailAddress: {
                            message: 'The input is not a valid email address'
                        },
                        remote: {
                        message: 'The email is already exist. you are already a registered user. please try to login?',
                        url: 'remote.php',
                        data:{
                        type:'email'
                        },
                        type: 'POST',
                        delay: 2000
                        }
                    }
                }
dinotom
  • 4,990
  • 16
  • 71
  • 139
  • 1
    code formatting is terrible. why do you expect someone to read that ? `select *` is bad; "javascript file" is not a complete javascript file. we have no idea what validator you're using for that object. validator key is `emaill[]` with two `l` characters but your input `name` property is `email[]` with one `l`. – Mulan Jun 03 '16 at 08:04
  • well, searching in php instead of sql is one way to avoid injections... – dandavis Jun 03 '16 at 08:08

1 Answers1

0

Why don't you do the check in your SQL query?

$sql = "SELECT email FROM members WHERE email = '".$_POST['email']."'";

Now you only have to check if it returns more than 0 results:

$valid = false;
$result=$db_handle->runQuery($sql);
if(sizeOf($results) > 0) {
    $valid = true;
}

Although you probably should escape the $_POST the avoid SQL injection ;-)

l0rdseth
  • 89
  • 9