dragon8997474 2018-06-20 07:49
浏览 74
已采纳

使用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 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>';
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面