dragon8997474
2018-06-20 07:49 阅读 73
已采纳

使用mysqli验证插入重复条目是不起作用的

I Have written a query for validating the email ids to not accept the duplicate emails while inserting into database.But it is not working and inserting duplicate email ids into database.

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条回答 默认 最新

  • 已采纳
    dongshi2836 dongshi2836 2018-06-20 07:57

    You are checking wrong email id. change $email = $_POST['user_email']; to $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>';
        }
      }
    }
    
    点赞 评论 复制链接分享

相关推荐