2014-02-08 134 views
0

這裏是我的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),我可以提醒正確的東西,使用函數來獲取發佈內容似乎是不對的,然後它不能被插入到數據庫中。

+0

除去「VALUERS」中的「R」有一點。你爲什麼要命名一個表「加入」。這是一個SQL關鍵字,並會造成相當混亂的查詢。 –

回答

0

的INSERT的關鍵詞是VALUESVALUERS

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

+0

我在我的問題中輸入了錯誤,如果我使用$ post_content ='hello,wolrd'爲$ post_content賦值,查詢會成功插入一行 –

+0

我們將不得不看到更多代碼,然後 –

0

錯字 -

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

也許是因爲你有一個錯字? INSERT INTO join估價師( '', '用戶名', '$ POST_CONTENT')應該是 INSERT INTO join VALUES( '', '用戶名', '$ POST_CONTENT') 我看不到任何東西錯

+0

是的,我在我的問題中輸入了錯誤,如果我使用$ post_content ='hello,wolrd'爲$ post_content賦值,查詢會成功插入一行,但如果我使用getpost()函數返回內容並賦值到$ post_content,它贏了;工作 –