【问题标题】:Validation for inserting duplicate entries is not working using mysqli使用 mysqli 插入重复条目的验证不起作用
【发布时间】:2018-06-20 07:49:58
【问题描述】:

我已经编写了一个查询来验证电子邮件 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>';

    }
 }

【问题讨论】:

  • 了解prepared Statements以防止SQL注入
  • 在插入中使用$_POST[email] 并在选择中使用$_POST['user_email'] 也许这就是原因

标签: php mysqli


【解决方案1】:

您正在检查错误的电子邮件 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>';
    }
  }
}

【讨论】:

  • 它工作正常,但如果我们输入重复的电子邮件 ID,它不会显示任何消息,因为电子邮件已经存在或不存在
  • 如果记录存在,那么它应该显示电子邮件已经存在的回显消息
  • 那么它是否符合条件?你检查了吗?
  • 是的,它正在检查电子邮件是否存在然后不插入数据库的条件
  • 好的,所以你不能在 thi 中回显消息。分享你的 html 代码,让我明白。
猜你喜欢
  • 2018-09-18
  • 2017-10-20
  • 1970-01-01
  • 2020-04-13
  • 2013-02-24
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多