2011-11-26 332 views
-1

所以我的問題是,由於某種原因,該項目似乎並沒有被進入我的數據庫:插入到MySQL數據庫

$comment = $_POST['comment']; 

if (isset($_POST['username']) or $_POST['username'] != '') { 
    $username = $_POST['username']; 
mysql_query('Insert into comments VALUES (comment="'.$comment.'", username="'. $username.'")'); 
echo $username; 
} 
else { 
    mysql_query('insert into comments values (comment="'.$comment.'")'); 
} 

我的表名爲意見,一個字段命名爲評論,一個字段被命名爲用戶名。

我的代碼有什麼問題?

+1

爲了記錄,該代碼具有非常高的SQL注入潛力。 **始終**過濾您的輸入。 –

回答

5

SQL插入格式不正確: -

insert into comments 
(list of columns...) values (list of values ...); 

或者

insert into comments 
set column1=value1, column2=value2 ...; 

如果你做一個var_dump(mysql_error());
你應該能夠看到錯誤

更詳細的細節題外話: -

您插入語句不迎合SQL注入,
https://stackoverflow.com/tags/php/info

+0

我知道,稍後會補充一點。 –

0

首先浮現在腦海中:

if (isset($_POST['username']) or $_POST['username'] != '') { 
    $username = $_POST['username'];//If you use OR in the condition. $username could be not set of maybe '' 

所以更換或與AND,所以當你拿到如果真實情況裏面,$用戶名將會有一些

0

更正後的代碼如下...... 我在以前的php項目中使用過這種技術.... 可能對您有用..

$comment = $_POST['comment']; 

if (isset($_POST['username']) AND $_POST['username'] != '') { 
    $username = $_POST['username']; 
mysql_query("Insert into comments VALUES ('".$comment."', '".$username."')"); 
echo $username; 
} 
else { 
    mysql_query("insert into comments values ('".$comment."')"); 
}