输入后不会显示更新的数据库(AJAX)

时间:2013-12-07 08:58:02

标签: javascript php jquery ajax database

我确信之前已被问过。但还没有找到我正在寻找的答案。提交信息后,我无法显示 UPDATED 数据库。我只能显示过时的数据库。数据库工作正常,我能够使用AJAX将我的条目放入数据库。 (我加载了php文件以查看它是否已更新,确实如此)。但我想在我的初始html页面上显示它。任何帮助表示赞赏。感谢。

我的html文件....

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
    function getDB(str) 
    {
        if (str == "") {
            document.getElementById("info").innerHTML = "";
            return;
        }
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        $(function () {
            $("#info").submit(function () {
                $.ajax({
                    type: "POST",
                    url: "phonebook.php",
                    data: $("#info").serialize(),
                    success: function () {
                        alert("Success!");
                    },
                    error: function () {
                        alert("Error!");
                    }
                });
                return false;
            });
        });

        xmlhttp.onreadystatechange=function()
        {
         if (xmlhttp.readyState == 4)
         {
             document.getElementById("info").innerHTML = xmlhttp.responseText;
         }
        }

       xmlhttp.open("GET","phonebook.php?q="+str,true);
       xmlhttp.send();

    }
</script>
</head>

<body>
&nbsp;<p></p> 
<form id="info">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td><h3></td><td>Last Name: <input type="text" size="20" id="lastname" name="lastname"></h3></td></tr>
<tr><td><h3></td><td>First Name: <input type="text" size="20" id="firstname" name="firstname"></h3></td></tr>
<tr><td><h3></td><td>Comment: <input type="text" size="20" id="comment" name="comment"></h3></td></tr>
<tr><td></td><td><input type="submit" value="Add" border="0" onclick="getDB(this.value)"></td></tr>
</table>
</form>
</body>

我的PHP文件......

<?php
//$q = intval($_POST['q']);
if(!($db=new SQLite3('sqlite/phonebook.sqlite3', '0666')))
    {
        echo "<h2>".$TEXT['phonebook-error']."</h2>";
        die();
    }
?>

<?php
echo "<h3>" .$TEXT['phonebook-head1']. "</h3>";
echo "<table border=0 cellpadding=0 cellspacing=0><tr bgcolor=red><td><img src=blank.gif width=10 height=25></td>";
echo "<td class=tabhead><img src=blank.gif width=150 height=6><br><b>" .$TEXT['phonebook-attrib1']. "</b></td>";
echo "<td class=tabhead><img src=blank.gif width=150 height=6><br><b>" .$TEXT['phonebook-attrib2']. "</b></td>";
echo "<td class=tabhead><img src=blank.gif width=150 height=6><br><b>" .$TEXT['phonebook-attrib3']. "</b></td>";
echo "<td class=tabhead><img src=blank.gif width=75 height=6><br><b>" .$TEXT['phonebook-attrib4']. "</b></td>";
echo "<td><img src=blank.gif width=10 height=25></td></tr>";
?>

<?php
$firstname=$db->escapeString(@$_REQUEST['firstname']);
$lastname=$db->escapeString(@$_REQUEST['lastname']);
$comment=$db->escapeString(@$_REQUEST['comment']);

if($firstname !="" && $lastname !="")
{
    $db->query("INSERT INTO 'Phone Book' (firstname,lastname,comment) VALUES('$firstname','$lastname','$comment')");
}

if(@$_REQUEST['action']=="del")
{
    $db->query("DELETE FROM 'Phone Book' WHERE id=".round($_REQUEST['id']));
}

$result=$db->query("SELECT id,firstname,lastname,comment FROM 'Phone Book' ORDER BY lastname;");

$i=0;
while( $row=$result->fetchArray(SQLITE3_ASSOC) )
{
    if($i>0)
    {
        echo "<tr valign=bottom>";
        echo "<td bgcolor=#ffffff background='border.gif' colspan=6><img src=blank.gif width=1 height=1></td>";
        echo "</tr>";
    }
    echo "<tr valign=center>";
    echo "<td class=tabval><img src=blank.gif width=10 height=20></td>";
    echo "<td class=tabval><b>".htmlspecialchars($row['lastname'])."</b></td>";
    echo "<td class=tabval>".htmlspecialchars($row['firstname'])."&nbsp;</td>";
    echo "<td class=tabval>".htmlspecialchars($row['comment'])."&nbsp;</td>";
    echo "<td class=tabval><a onclick=\"return confirm('".$TEXT['phonebook-sure']."');\" href=phonebook.php?action=del&id=".$row['id'].">[".$TEXT['phonebook-button1']."]</a></td>";
    echo "<td class=tabval></td>";
    echo "</tr>";
    $i++;

}

echo "<tr valign=bottom>";
echo "<td bgcolor=red colspan=6><img src=blank.gif width=1 height=8></td>";
echo "</tr>";

$db->close();
?>

1 个答案:

答案 0 :(得分:0)

数据库,表和列名称不能以空格字符结尾。

数据库和表名称不能包含“/”,“\”,“。”或文件名中不允许的字符。

标识符引号字符是反引号(“`”)。

 $db->query("DELETE FROM `Phone Book` WHERE id='".round($_REQUEST['id'])."' ");

参考:http://dev.mysql.com/doc/refman/5.0/en/identifiers.html