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 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)