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

时间:2013-08-07 16:30:16

标签: php javascript jquery mysql ajax

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

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

答案 0 :(得分: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);
}
?>

答案 1 :(得分:0)

我看到你的帖子网址是:     URL: “formpost”

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

答案 2 :(得分:0)

首先要做的是查看返回的data的值是多少。这会让你知道问题出在哪里。此外,最好使用count()而不是获取所有行,以便您可以执行mysql_num_rows()

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

将返回单元格等于$cell的行数。解释该结果,然后回应您的需求。还要检查查询是否成功(处理错误)。如果未设置$_POST['cell'],则没有理由在数据库中查询null的单元号,只需立即返回所需的值。你也有评论说你很容易被SQL注入,所以你也应该考虑修复它。

答案 3 :(得分: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 } ?>

这应该意味着只有在你没有帖子价值时才会写出javascript。

相关问题