检查数据库中是否存在单元格编号


Check if cell number exists in database

我想检查用户试图输入的单元格编号是否已经存在。但下面的代码总是称之为"使用中的手机号码",

我做错了什么?仍然从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>

这应该有效:

<?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);
}
?>

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

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

首先要做的是查看返回的data的值。这会让你知道问题出在哪里。此外,使用count()比仅仅为了执行mysql_num_rows()而获取所有行要好。

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

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

如果您在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。