如何修复转义字符串

时间:2014-07-28 08:24:21

标签: php

我只是创建一个评论表单,我偶然发现了这个错误

mysql_real_escape_string() expects parameter 1 to be string, resource given in D:\xampp\htdocs\test\pages\blogprocessing.php on line 61

我真的不知道为什么它会给我它,我不是PHP专家,但我真的可以帮助你们帮助..

我的部分代码......

    <?php 
 $comment = mysql_real_escape_string($connection, $_POST['comment']); 
 $blogID = $_POST['blogID']; //retrieve the blogID from the hidden form field
 $authorID = $_SESSION['username']; 

 $sql = "INSERT INTO comment (blogID, authorID, datePosted, comment) VALUES 
('$blogID', '$authorID', NOW(), '$comment')"; //sql query
$result = mysql_query($sql) or die(mysql_error($connection)); //run the query 

谢谢! :)

&GT;

3 个答案:

答案 0 :(得分:0)

错误很明显。 mysql_real_escape_string的第一个参数不是字符串。

正如文档所说(http://ie2.php.net/mysql_real_escape_string),第一个参数必须是转义字符串,第二个参数必须是资源标识符。

好方法是:

$comment = mysql_real_escape_string($_POST['comment'], $connection); 

答案 1 :(得分:0)

看看PHP手册和你的错误:

mysql_real_escape_string首先将参数设为字符串,而不是连接处理程序。

http://php.net/manual/de/function.mysql-real-escape-string.php

答案 2 :(得分:0)

我同意前面的发言者。虽然仔细阅读文档:

Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

强烈建议删除mysql支持!使用像mysqli这样的库,它也有转义字符串。链接如下。

http://pl1.php.net/manual/en/mysqli.real-escape-string.php