0

我想检查用户尝试输入的手机号码是否已经存在。但是下面的代码总是称这个“正在使用的单元格号”,

我做错了什么?还是从php开始。

<?php
mysql_connect("localhost", "root", "password", "users");
mysql_select_db("users");
$cell = (isset($_POST["cell"])?
$_POST["cell"] : null);
$query=mysql_query("SELECT * from users where cell='$cell' ");
$find=mysql_num_rows($query);
echo $find;
?>

<script>
 $(document).ready(function(){
        $("#Cell").blur(function(){
            $("#Status_Cell").show();
             $("#Status_Cell").html("checking...");
        var cell = $("#Cell").val();
          $.ajax({
                type:"post",
                url:"formpost",
                data:"cell="+cell,
                    success:function(data){
                    if(data==0){
                        $("#Status_Cell").html("Cell Number available");
                    }
                    else{
                        $("#Status_Cell").html("Cell Number in use");
                    }
                }
             });

        });

     });
</script>
4

4 回答 4

1

这应该有效:

<?php
if(isset($_POST['cell'])) {
    mysql_connect("localhost", "root", "password", "users");
    mysql_select_db("users");
    $cell = (isset($_POST["cell"])?
    $_POST["cell"] : null);
    $query=mysql_query("SELECT * from users where cell='$cell' LIMIT 1");
    $find=mysql_num_rows($query);
    die($find);
}
?>

<script>
 $(document).ready(function(){
        $("#Cell").blur(function(){
            $("#Status_Cell").show();
             $("#Status_Cell").html("checking...");
        var cell = $("#Cell").val();
          $.ajax({
                type:"post",
                url:"formpost",
                data:"cell="+cell,
                    success:function(data){
                    if(data==0){
                        $("#Status_Cell").html("Cell Number available");
                    }
                    else{
                        $("#Status_Cell").html("Cell Number in use");
                    }
                }
             });

        });

     });
</script>

因此,如果您将“数据”发送到脚本,它将查找结果、输出计数并退出。

如果没有,它将显示页面的其余部分。

还要注意我是如何将 LIMIT 1 放在那里的,只是进行了一些性能优化,因为您只想知道数据是否存在。

编辑:就性能和安全性而言,这是一种更好的方法:

<?php
if(isset($_POST['cell'])) {
    mysql_connect("localhost", "root", "password", "users");
    mysql_select_db("users");
    $cell = $_POST["cell"];
    $query = mysql_query("SELECT COUNT(*) AS number from users where cell='".mysql_real_escape_string($cell)."' LIMIT 1");
    $find = mysql_result($query, 0);
    die($find);
}
?>
于 2013-08-07T16:53:10.367 回答
0

如果您在 ajax 调用中从同一页面接收结果,就好像您已经浏览到该页面一样,因此返回的内容将包括页面上的所有内容,包括您的 javascript。尝试使用不同的页面作为 ajax 目标或执行以下操作:

<?php

if(isset($_POST["cell"]))
{
    mysql_connect("localhost", "root", "password", "users");
    mysql_select_db("users");
    $cell = $_POST["cell"];
    $query=mysql_query("SELECT * from users where cell='$cell' ");
    $find=mysql_num_rows($query);
    echo $find;
}
else 
{
?>

    <script>
     $(document).ready(function(){
            $("#Cell").blur(function(){
                $("#Status_Cell").show();
                 $("#Status_Cell").html("checking...");
            var cell = $("#Cell").val();
              $.ajax({
                    type:"post",
                    url:"formpost",
                    data:"cell="+cell,
                        success:function(data){
                        if(data==0){
                            $("#Status_Cell").html("Cell Number available");
                        }
                        else{
                            $("#Status_Cell").html("Cell Number in use");
                        }
                    }
                 });

            });

         });
    </script>

<?php } ?>

这应该意味着只有在您没有 post 值的情况下才会写出 javascript。

于 2013-08-07T16:54:20.437 回答
0

首先要做的是查看data返回的值是多少。这会让你知道问题出在哪里。此外,使用它count()比获取所有行更好,这样你就可以做到mysql_num_rows()

SELECT COUNT(*) from users where cell='$cell'

将返回单元格等于的行数$cell。解释该结果,然后回显您需要的内容。还要检查查询是否成功(处理错误)。如果$_POST['cell']未设置,则没有理由查询数据库中的单元格编号,即null立即返回所需的值。您还收到评论说您容易受到 sql 注入的影响,因此您也应该考虑修复它。

于 2013-08-07T16:49:03.453 回答
0

我看到你的帖子网址是: url:"formpost"

如果 js 代码和 php 在同一个文件中,ajax 的返回数据将是整个 html 和 javascript,而不仅仅是 "echo $find;";当然你的数据永远不会是0;

于 2013-08-07T16:44:43.057 回答