2011-07-11 111 views
1

所以我有这样的代码,需要消息/发布用户插入和它的意图将其发布到数据库,然后显示和单独的页面。香港专业教育学院得到了展示园做工精细它只是试图插入到数据库这是问题
这个代码...发布到数据库

<?php 
mysql_connect("localhost", "root", ""); 
mysql_select_db("test");  
$time = time(); 
mysql_query "INSERT INTO threads (title, message, author, dated);" 
VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time');  
echo "Thread Posted.<br><a href='Untitled9.php'>Return</a>"; 
?> 

的信息来源不会上传到数据库中!

这是为什么?如何解决?

id  int(11)       No None AUTO_INCREMENT    
    title varchar(255) latin1_swedish_ci No None     
    message text   latin1_swedish_ci No None     
    author varchar(255) latin1_swedish_ci No None     
    replies int(11)       No None     
    posted varchar(255) latin1_swedish_ci No None     
    votes_up int(11)       No 0     
    votes_down int(11)      No 0 
+3

亲爱的可爱的小孩耶稣请净化你的输入。 –

+1

我想我每次看到这么多的sql注入点时都会死在里面。 – Cyclone

+0

我还没有了解SQL注入。这还没有在网络上,我希望在完成所有工作后防止黑客入侵 – louismoore18

回答

2

更新:

应张贴不注明日期。

继承人的问题:

mysql_query "INSERT INTO threads (title, message, author, posted);" 
VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time'); 

将其更改为:

mysql_query("INSERT INTO threads (title, message, author, posted) VALUES ('$_POST[title]','$_POST[message]','$_POST[author]','$time');"); 

我看你有没有空值也,这让我相信,使用ID有自动递增的,如果这是这种情况下,你也需要提供这个。例如:

编辑:这里

mysql_query("INSERT INTO threads (id,title, message, author, posted) VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','$time');"); 

注直接从POST数据是不安全的,让你开到各种攻击插入值。

+0

它需要什么? – louismoore18

+0

检查编辑。 – Eddie

+0

刚刚看到..你的代码带走了错误代码,但没有任何东西仍在发布:/ – louismoore18

0
mysql_query "INSERT INTO threads (title, message, author, dated);" 
VALUES ('$_POST[title]','$_POST[message]','$_POST[author]','$time'); 

您结束了字符串提前。应该是:

mysql_query("INSERT INTO threads (title, message, author, dated) 
    VALUES ('$_POST[title]','$_POST[message]','$_POST[author]','$time')"); 

此外,您的代码很可能成为SQL注入的目标。您应该使用MySQLi级别和PreparedStatement插入您的帖子。

+1

你的代码仍然是错误的,太多的价值观。 – Eddie

+0

Arg,你说得对。纠正了这一点。 –

0

您尝试添加到新行的值多于指定的值。

mysql_query "INSERT INTO threads (title, message, author, dated);" 

是4个值要设置

VALUES (NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time'); 

,并要指定6个值。

这是不可能

此外验证$ _POST数据=阅读本Never trust user input

而且阅读手册PHP & MYSQL

0

分号即将结束你的SQL statment。您的查询未完成。你仍然需要指定你想要插入的值。问题

0

编号:

  1. 如果你把$ _ POST [],你需要把它放在括号{$ _ POST []}或PHP将无法破译的变量
  2. 下一个名称的字符串中需要引用$ _POST []中的变量,以便PHP不认为它们是CONSTANTS,所以它们需要像$ _POST ['title']或$ _POST [“title”]
  3. 其他人有说你需要通过过滤发布的变量来防止SQL注入。最安全的做法是使用PDO,我在下面列出了一个例子。你可以改善这一点。
  4. 打开错误报告,所以你可以看到的错误,同时调试

这里的测试代码:

ini_set('error_reporting', E_ALL | E_STRICT); 
ini_set('display_errors', 'On'); 
$user='root'; 
$pass=''; 
$dsn = 'mysql:dbname=test;host=localhost'; //for PDO later 

mysql_connect("localhost",$user , $pass); 
mysql_select_db("test");  
$time = time(); 
if (isset($_POST) && !empty($_POST)) 
{ 
// using braces {} 
$sql=<<<SQL 
INSERT INTO threads (title, message, author, posted) 
VALUES ('{$_POST['title']}','{$_POST['message']}','{$_POST['author']}','$time') 

SQL; 

echo "$_POST[title]"."Thread Posted.<br><a href='Untitled9.php'>Return</a>"; 

// now a PDO version of the same 
try { 
    $pdo = new PDO($dsn, $user, $pass); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage();die; 
} 

$sth = $pdo->prepare("INSERT ino threads (title, message, author, posted) 
        VALUES (:title,:message,:author,:posted)"); 
$sth->execute(array(':title' => $_POST['title'],':message' => $_POST['message'], ':author' => $_POST['author'] ,':posted' => $time)); 
echo "Affected rows=".$sth->rowCount().",we are on line=".__LINE__."<br />"; 
echo $_POST['title']." Thread Posted.<br><a href='Untitled9.php'>Return</a>"; 

} // close if $_POST