php - SQL query validation - not testing each time -


i've got php page javascript passing in string of values inputs aa insert.php page.

on insert.php page, test whether or not email passed in exists in database. query works in regard, happens error gets passed page , outputs validation error.

what want happen after validation error sends it's output. user can change field , try again. reason, insert.php page doesn't check again this, , if does, won't let user input different value continue. (the error message stays on screen , won't allow unique inputs through)

what's wrong sql query/php?

$errors = array();  $sql="select 1 data_table email = '".$email."' limit 1"; $sth = $db->prepare($sql); $varcomp = $sth->execute(); if ($varcomp != '0') {     $errors[] = '<span style="color:red;">limit 1 entry per person.</span>'; }   if (sizeof($errors) > 0) {     $result = array(     "errors" => implode("<br/>", $errors),     "success" => 0);     die(json_encode($result)); } 

to reiterate, sql query works fine first time through validating whether or not email in data table. however, after spits error out on page "limit 1 entry per person", if user changes email unique, error message doesn't go away , won't allow new unique email either checked again or submitted.

change query code this:

$sql="select email data_table email = ? limit 1"; $sth = $db->prepare($sql); $sth->execute(array($email)); if ($sth->rowcount() != 0) {     $errors[] = '<span style="color:red;">limit 1 entry per person.</span>'; } 

using named placeholders:

$sql="select email data_table email = :email limit 1";  $sth = $db->prepare($sql);  $sth->execute(array(':email'=>$email)); 
  • you need count rows, not check if query successful
  • use prepared statement

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -