使用mysqli验证插入重复项不起作用

时间:2018-06-20 07:49:58

标签: php mysqli

我编写了一个查询,用于验证电子邮件ID,以便在插入数据库时​​不接受重复的电子邮件。但是,它无法正常工作,并且无法将重复的电子邮件ID插入数据库。

if(isset($_POST['submit_user'])){

     $email = $_POST['user_email'];
      $check=mysqli_query($conn,"select * from users where user_email='$email'");
      $checkrows=mysqli_num_rows($check);
      if($checkrows>0) {
  echo "Email Already exists";
   } else {  

    if($_POST['password'] == $_POST['con_password']){

        $date = date('Y-m-d h:i:s');

        $ins_sql = "INSERT INTO users (first_name, last_name, user_email, user_password, user_gender, user_marital_status, user_phone_no, user_designation,user_address,user_date,user_role,username) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[email]', '$_POST[password]', '$_POST[gender]', '$_POST[marital_status]', '$_POST[phone_no]', '$_POST[designation]', '$_POST[address]', '$date','$_POST[user_role]' , '$_POST[username]')";

        $run_sql = mysqli_query($conn,$ins_sql);

    }else {

        $match = '<div class="alert alert-danger">Password doesn&apos;t match!</div>';

    }
 }

1 个答案:

答案 0 :(得分:0)

您正在检查错误的电子邮件ID。将$email = $_POST['user_email'];更改为$email = $_POST['email'];

if(isset($_POST['submit_user'])){

   $email = $_POST['email'];
   $check=mysqli_query($conn,"select * from users where user_email='$email'");
   $checkrows=mysqli_num_rows($check);
   if($checkrows>0) {
      echo "Email Already exists";
   } else {
    if($_POST['password'] == $_POST['con_password']){
        $date = date('Y-m-d h:i:s');
        $ins_sql = "INSERT INTO users (first_name, last_name, user_email, user_password, user_gender, user_marital_status, user_phone_no, user_designation,user_address,user_date,user_role,username) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[email]', '$_POST[password]', '$_POST[gender]', '$_POST[marital_status]', '$_POST[phone_no]', '$_POST[designation]', '$_POST[address]', '$date','$_POST[user_role]' , '$_POST[username]')";
        $run_sql = mysqli_query($conn,$ins_sql);
    }else {
        $match = '<div class="alert alert-danger">Password doesn&apos;t match!</div>';
    }
  }
}