2012-04-13 83 views
0

我有一个MySQL声明是这样的:特殊字符打破MySQL的插入

mysql_query("INSERT INTO movies (comments, description, synopsis) 
VALUES ('$_POST["comments"]', '$_POST["desc"]',$_POST["synopsis"])"); 

非常简单明了的,你可以看到。问题是当我向表单输入特殊字符时,它不会将数据插入到我的表中(使用phpmyadmin直接检查它是否被插入)。 例如,如果我把评论textarea这个值:“这是一条评论” 这个作品 如果我把,而不是:“你叫什么名字?:约翰doe是我的名字” 它breaks.I知道它是因为mysql使用人物......我应该做什么的建议?

+5

请从这里开始:[大逃避现实(或:你需要知道的处理文本中的文本)](http://kunststube.net/escapism/) – deceze 2012-04-13 01:11:30

回答

7
mysql_query("INSERT INTO movies (comments, description, synopsis) 
VALUES ('".mysql_real_escape_string($_POST["comments"])."', '".mysql_real_escape_string($_POST["desc"])."','".mysql_real_escape_string($_POST["synopsis"])."'"); 
-1
$comments = mysql_real_escape_string($_POST['comments']); 
$desc = mysql_real_escape_string($_POST['desc']); 
$synopsis = mysql_real_escape_string($_POST['synopsis']); 

mysql_query("INSERT INTO movies (comments, description, synopsis) 
VALUES ('$comments', '$desc', '$synopsis'"); 

欲了解更多信息,谷歌“的PHP和addslashes”,或者看看这个页面看上去解释http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

+0

为什么'addslashes'而不是相关的mysql函数? – deceze 2012-04-13 01:20:47

+0

更改为“mysql_real_escape_string”。我把“google for php addslashes”只是为了引导更多的信息,因为这个原因。 – 2012-04-13 01:22:19

0
$comments = mysql_real_escape_string($_POST['comments']); 
$desc = mysql_real_escape_string($_POST['desc']); 
$synopsis = mysql_real_escape_string($_POST['synopsis']); 

mysql_query("INSERT INTO movies (comments, description, synopsis) 
VALUES ('$comments', '$desc', '$synopsis'");