2011-11-26 181 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."')"); 
}