200 OK错误AJAX - MYSQL请求+“未定义”返回

时间:2011-12-01 00:39:50

标签: php mysql ajax

我正在尝试实现一个AJAX应用程序,其中用户输入用户的id,与id相关的所有信息都将填入剩余的文本字段中。例如,当用户输入id号“1234”时,姓氏,名字,喜欢的颜色和喜欢的号码字段会自动填充mysql数据库中找到的该id的信息。从那里,用户将能够看到与该特定id相关的所有当前信息并进行更改。

下面是我的脚本,但是当我运行它时,返回的所有值都是“undefined”。当我通过firebug运行错误时,会产生一个GET响应:响应只是读取:

  

来自id = \“$ id \”

的用户的SELECT *

知道可能出现什么问题吗?

<html>
    <head>
    <script type = "text/javascript">
        var xhr;
        if (window.ActiveXObject)
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            xhr = new XMLHttpRequest();
        }

        function callServer()
        {
            // Create the id number
            var id = document.getElementById("id").value;

            // Create regular expressions
            var reg1 = /\d{4}/;

            // Test the string against the regular expression
            if (reg1.test(id))
            {
                var idCheck = id;
            }
            else
            {
                alert ("Invalid id number");
                return;
            }

        // Build the URL to connect to
        var url = "(restofurl).../dataExtract.php?idCheck=" +escape(idCheck);

        // Open a connection to the server
        xhr.open("GET", url, true);

        // Setup a function for the server to run when it is done
        xhr.onreadystatechange = updatePage;

        // Send the request
        xhr.send(null);
    }

    function updatePage()
    {
        if ((xhr.readyState == 4) && (xhr.status == 200))
        {
        var response = xhr.responseText.split(",");
        var reg3 = /\S+/;
            if (!reg3.test(document.getElementById("lastname").value))
        {
                document.getElementById("lastname").value = response[1];
        }
            if (!reg3.test(document.getElementById("firstname").value))
        {
                document.getElementById("firstname").value = response[2];
        }
            if (!reg3.test(document.getElementById("color").value))
        {
                document.getElementById("color").value = response[3];
        }
            if (!reg3.test(document.getElementById("number").value))
        {
                document.getElementById("number").value = response[4];
        }
        }
    }
</script>
</head>
<body>
    <form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
        <p>Update Database:</p>

        <p>ID:<input type = "id" id="id" name="id" size="20" maxlength="40" onchange = "callServer();" /></p>
        <p>Update Last Name:<input type = "lastname" id="lastname" name="lastname" size="20" maxlength="40" /></p>
        <p>Update First Name:<input type = "firstname" id="firstname" name="firstname" size="20" maxlength="40" /></p>
        <p>Update Favorite Color:<input type = "color" id="color" name="color" size="20" maxlength="40" /></p>
        <p>Update Favorite Number:<input type = "number" id="number" name="number" size="20" maxlength="40" /></p>
       <input type="submit" id="submit" name ="submit" value="Update" /><br><br>


    </form>
</body>
</html>

dataExtract.php

<?php

// Connect to the MySQL database
$con = mysql_connect("server","user","password");

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("tablename", $con);

$id = $_GET["id"];

$table = "users";
$query = 'SELECT * from $table where id = \"$id\"';
$result = mysql_query($query) or trigger_error(mysql_error().$query);

if (empty($result))
{
    $response = " , , , ";
}
else
{
    $row = $result->fetch_row();
    $response = $row[1].",".$row[2].",".$row[3].",".$row[4];
    echo $response;
}


// Close connection to the database
mysql_close($con);
?>

1 个答案:

答案 0 :(得分:2)

您在查询中使用单引号中的$ table,导致MySQL执行'select * from $ table ...'。类似地,您在双引号中添加了反斜杠,当您使用单引号指定字符串文字时,这不需要。