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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用