简体   繁体   中英

Registration form: problems with email check

i have a problem with the realization of a registration form. My php script should check if the user email is already in use. if the email is in use the php script should show an error message, if it is not the registration is successfully completed.

$email = $_POST['email'];       
  try{            
        $sql = "SELECT count(mail) FROM user WHERE mail = '$email'";            
        $result = $pdo->exec($sql);        
  }catch(PDOException $e){            
      echo $e;            
      exit();        }
  if($result == 0){            
     //registration complete        }
  else{            
      //email already in use        } 

my problem is that i obtain always 0 as result also if the email is already inside the database. But if i execute that sql code inside my xampp' server i obtain 1 so the code works perfectly.

Thank you to all for help :)

You need to do this:

$email = $_POST['email'];       
  try{            
        $sql = "SELECT mail FROM user WHERE mail = :email";   
        $sql = $pdo->prepare($sql);
        $sql->execute(array(':email'=> $email));       
  }catch(PDOException $e){            
      echo $e;            
      exit();        }
  if($sql->rowCount() == 0){            
     //registration complete        }
  else{            
      //email already in use        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM