我正在创建一个类似facebook的发布系统。。
我今天的问题是,它似乎没有将我从文本区域获得的值插入到我的数据库中。。
这是我的java脚本:
$("#share").click(function()
{
//var typeNew = document.getElementById("content").value;
var update = $( "textarea#content" ).val();
//document.write(update);
if(update.length == 0)
{
alert("empty, please type something.");
//$(this).html('<meta http-equiv='"Refresh'" content='"1; URL=insert.php'">');
}
else
{
//$("#flash").show();
$("#flash").html('<img src="loader.gif" />Loading Comment...').fadeIn("slow");
$.ajax({
type: "POST",
url: "post_update.php",
data: 'update=' + update,
success: function(msg)
{
$("#flash").ajaxComplete(function(event, request, settings){
//alert("Successfully Inserted")
$("#flash").hide();
//$(this).html('<meta http-equiv='"Refresh'" content='"1; URL=insert.php'">');
});
}
});
}
return false;
});
然后是我的php代码:
<?php
$post=$_REQUEST['update'];
$post=$_POST['update'];
//echo '$post';
@ $db = new mysqli('localhost', 'root', '', 'wall');
if(mysqli_connect_errno())
{
echo "Error! Could not connect to database. Reset fields.";
exit;
}
$sql = "INSERT INTO posts(update,date_posted) VALUES('$post',NOW())";
$result = $db->query($sql);
if($result){
echo 'OK';
}
else{
echo 'FAIL';
}
$db->close();
?>
有人能告诉我怎么了吗?当delete函数出错时,它工作得很好,但现在它正常了,我的共享函数就不工作了。。
在您的PHP代码中,您有以下行:
$post=$_REQUEST['update'];
$post=$_POST['update'];
你不应该两者都有。在您的情况下,您实际上只需要第二个,但为了进行测试,请尝试注释掉它,只留下$_REQUEST
行。现在您也可以通过GET传递参数。
要查看查询是否正确,请将其打印出来,如下所示:
echo $sql = "INSERT INTO posts(update,date_posted) VALUES('$post',NOW())";
现在将您的浏览器指向your.domain/post_update.php?update=testmessage
位置并查看输出。
如果一切正常,请将POST/REQUEST行替换为:
$post=$db->real_escape_string($_POST["update"]);
前几天我遇到了这个。使用自动提交或启动并提交事务。另外,在语句末尾使用分号(可能不是问题所在)。
http://dev.mysql.com/doc/refman/5.0/en/commit.html
如果你的$post表现良好,那么试试:
$post = $db->real_escape_string($_POST["update"]);
if (!db->query("INSERT INTO posts(update,date_posted) VALUES('$post' ,NOW())")) {
echo $db->sqlstate; //show error
}
else {
echo "inserted";
}
假设update
的列类型为varchar
/char
,date_posted
为datetime
:
$sql = sprintf("INSERT INTO posts(update,date_posted) VALUES('%s',NOW())",
mysql_real_escape_string($post));
$result = $db->query($sql);
请将列名"update"更改为其他列名。它可能起作用。并避免列名称的某些预定义变量。
希望能有所帮助。