错误电子邮件已存在


error email already exists

我从堆栈溢出的某人那里得到了这段代码,我复制了它并尝试了一下,但是如果我从数据库中输入已经存在的电子邮件地址,我无法按照我想要的方式工作,它会给出一个错误,说电子邮件地址已被占用,请选择另一封电子邮件。 但是当我提交不在数据库中的电子邮件地址时,它仍然显示相同的错误,这怎么可能固定

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){ //newly added 
$('#submit').click(function() {alert("Email already exists. Please choose a different email");
var emailVal = $('#email').val(); // assuming this is a input text field
$.post('checkemail.php', {'email' : emailVal}, function(data) {
  if (data == 1)
  { 
     $("#registration").submit();
  }
 else
  {
      return false;
  }
 });
 });});
 </script>
  </head>
 <body>
 <form id="registration" name="registration" method="post" action="profile1.php">
 <p>email
 <input type="text" name="email" id="email" />
 </p>
 <p>
 <input type="button" name="submit" id="submit" value="submit" />
 </p>
 </form>
</body>
</html>

这是PHP代码

<?php
include("con.php");
$sql = "SELECT email FROM registered_d WHERE email = " .$_POST['email'];
$select = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($select);
   if (mysqli_num_rows > 0) {
  echo "0"; // email exists
 }else{
  echo "1"; // email doesn't exists
 return;}
?>

有人可以帮我解决这个问题吗

mysqli_num_rows是一个

函数,因此应该这样使用 — myseli_num_rows($select)

另请记住,在未转义SQL查询中使用$_POST['email']是一种糟糕且危险的做法。

如果你不知道你是否有,为什么要mysqli_fetch_assoc()

问题是您在按下提交按钮时调用alert("Email already exists. Please choose a different email");。无论如何,每次都会生成警报。

$('#submit').click(function() {alert("Email already exists. Please choose a different email");

这将在每次单击提交按钮时显示一条警报,其中包含电子邮件已存在的消息。您需要移动它,如下所示。

$(document).ready(function(){ //newly added 
    $('#submit').click(function() {
        var emailVal = $('#email').val(); // assuming this is a input text field
        $.post('checkemail.php', {'email' : emailVal}, function(data) {
            if(data=='exist') {
                alert("Email already exists. Please choose a different email");
                return false;
            } else {
                $('#registration').submit();
            }
        });
    });
});

看起来"mysqli_num_rows"是一个变量,可能不是获取的结果。$row结果,所以也许你应该检查一下条件。

据此,行应该是一个数组:

http://php.net/manual/en/function.mysql-fetch-assoc.php

像这样:

if (mysqli_num_rows($rows) > 0) {
  echo "exist";
}else echo 'notexist';

使用这个

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
   $(document).ready(function(){ //newly added 
        $('#submit').click(function() {
            var emailVal = $('#email').val(); // assuming this is a input text field
            $.post('checkemail.php', {'email' : emailVal}, function(data) {
                if(data.message == "exist")
                    alert("This email address already exists");
                else
                    $('#registration').submit();
            });
            return false;
        });
    </script>

        </head>
        <body>
         <form id="registration" name="registration" method="post" action="profile1.php">
         <p>email
         <input type="text" name="email" id="email" />
         </p>
         <p>
         <input type="submit" name="submit" id="submit" value="submit" />
         </p>
         </form>
        </body>
        </html>

在你的 php 代码中

<?php
include("con.php");
$sql = "SELECT email FROM registered_d WHERE email = " .$_POST['email'];
$select = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($select);
 if (mysqli_num_rows($select) > 0) {
   echo json_encode(array("message"=>"exist"));
 }
 else echo  echo json_encode(array("message"=>"notexist"));
?>