无法将新行插入数据库

时间:2014-02-08 13:56:17

标签: php jquery mysql

这是我的php:

<?php
require included.php; //which contains the function getpost($post_id)
//once I use the getpost() function below to return a string which is the post 
//content, then I can not insert this new row to database.if I change it something 
//like $post_content='this is the post content' , it would insert a row to database

$post_content=getpost($post_id);
if($insert=mysql_query("INSERT INTO `join` VALUES('','username','$post_content')"))
{
   echo 'ok';
}
else{

   echo 'error';
}
?>

以下是included.php中的函数getpost($post_id)

function getpost($post_id){

    $select_post=mysql_query("SELECT `post_text` FROM `user_post` WHERE `id`='$post_id' ");
return mysql_result($select_post,0,'post_text');
}

这些数据将作为数据发送回jquery AJAX请求,如果我回显$post_content = get_post($post_id),我可以提醒正确的东西,使用函数获取帖子内容似乎有些不对,那么它可以不要插入数据库。

3 个答案:

答案 0 :(得分:0)

INSERT的关键字为VALUES而不是VALUERS

if($insert=mysql_query("INSERT INTO `join` VALUERS('','username','$post_content')"))

应该是

if($insert=mysql_query("INSERT INTO `join` VALUES ('','username','$post_content')"))

如果您进行了错误检查,例如使用mysql_error(),您可能会很快发现错误。

此外,please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

错字 -

...INSERT INTO `join` VALUERS('','usern...
                           ^

答案 2 :(得分:0)

也许是因为你有拼写错误? INSERT INTO join VALUERS('','username','$ post_content')应该是 插入join VALUES('','用户名','$ post_content') 我看不出别的错误