如何从查询中排除特定选择

时间:2019-08-23 12:09:08

标签: php sql

我具有此功能,可用于在注册表中检查电子邮件,如果当前正在使用已提交的电子邮件,则会禁用“提交”按钮。所以我想在配置文件编辑部分中修改该功能。我在profile.php中也有个人资料编辑表单。因此,当用户在不触摸电子邮件输入的情况下编辑其信息时,它就可以正常工作。但是,一旦用户单击并转到“电子邮件”字段,并且甚至不进行任何编辑就模糊了鼠标,它就会显示“电子邮件已被接收”错误。好的原因函数适用于注册表。因此,当用户提交不同的电子邮件时,我想检查它是否已经存在于db中,否则,如果他不进行任何更改,我什么都不显示然后继续。

我尝试通过以下查询在后端解决此问题:

  SELECT * FROM users WHERE email = ? AND id != ?
 .

 .

 .
 mysqli_stmt_bind_param($stmt, "ss", $email, $uid); 

但这不起作用

然后我尝试了SELECT * FROM users WHERE email =?

并遍历选择以检测是否存在id = uid的选择。如果是,则将结果变量设置为0。但这也不起作用。

function profEmailCheck(){
    $('#email').blur(function(){

       var email = $(this).val();
       var uid = $('#uid').val(); 

       var update_email_check = '';
       $.ajax({
        url:'update_email_check.php',
        method:"POST",
        data:{
            update_email_check: update_email_check,
            email: email,
            uid: uid
        },
        success:function(data)
        {
         if(data != 0)
         {
          $('.email-availability').html('<span class="text-danger">Email Already Taken</span>');
          $('#update-prof-btn').attr("disabled", true);
         }
         else
         {
            if (email == '') 
            {
              $('.email-availability').html('');  
              $('#update-prof-btn').attr("disabled", true);  
            }
            else
            {
              $('.email-availability').html('<span class="text-success">Email Available</span>');
              $('#update-prof-btn').attr("disabled", false);    
            }


         }
        }
       })

    });

  }
<?php

include('db_connect.php');

if (isset($_POST["update_email_check"])) {
    $email = mysqli_real_escape_string($conn, $_POST["email"]);
    $uid = mysqli_real_escape_string($conn, $_POST["uid"]);

    $sql = "SELECT * FROM users WHERE email = ? AND id != ?";
    $stmt = mysqli_stmt_init($conn);
    $query = mysqli_query($conn, $sql);

    if (!mysqli_stmt_prepare($stmt, $sql)) {
        echo "Something went wrong :(";
        exit();
    } else {
        mysqli_stmt_bind_param($stmt, "ss", $email, $uid);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
    }

    $result = mysqli_sql($conn, $sql);

    while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        if($rows['id'] == $uid){
            $result = '0';
        }
    }
    echo mysqli_num_rows($result);
}

1 个答案:

答案 0 :(得分:0)

据我了解,您想检查电子邮件控制。

If (exists){
Do something
}
else {
Do something
}

如果这是您想要的,那么您应该;

$ControlMailQuery = "SELECT * FROM users WHERE email = ?";
$ControlMailQueryResult = mysqli_query($db, $ControlMailQuery);
            if(mysqli_num_rows($ControlMailQueryResult) == 1){
//There is a one user who has this email
}
else{
//this is unique e-mail address
}