【发布时间】:2014-08-21 17:22:59
【问题描述】:
我目前遇到这个问题,我使用 mysqlescapestring 将数据插入到我的数据库中,并且在检索数据时包含许多冗余 / 字符。有没有办法扭转这种情况?
这是我的代码
$post_nameE = EscapeString($post_name);
$post_descE = EscapeString($post_desc);
$post_dateE = date("Y-m-d H:i:s");
$post_contentE = EscapeString($post_content);
$statusE = EscapeString($status);
$tagE = EscapeString($tag);
$sql="INSERT INTO post (post_id, post_name, post_desc, post_date, post_content, status, tags)
VALUES (DEFAULT, '$post_nameE', '$post_desc', '$post_dateE', '$post_contentE', '$statusE', '$tagE' )";
$res = dbQuery($sql);
if ($res)
echo "1 record added";
else
die('Error: ' . mysqli_error($res));
并定义转义字符串:
function EscapeString($s) {
global $mysqli;
return $mysqli->real_escape_string($s);
}
内容:
'The ice bucket challenge'
从数据库中检索并显示的内容:
\'The ice bucket challenge\'
我一直在阅读其他 stackoverflow 问题,大多数人声称这不应该发生,有解决办法吗?
【问题讨论】:
-
请贴出
EscapeString()的函数定义,因为这显然是用户定义的函数。我们需要看看那里具体发生了什么。此外,如果您有较旧的 PHP 版本,ensure thatmagic_quotes_gpcis disabled -
@MichaelBerkowski 问题已更新
-
当数据输入数据库时,您通过 real_escape_string 添加的任何转义都将消失。如果你得到了额外的转义,那么有些东西已经为你转义了你的文本,你实际上是在双重转义。
-
@Hanky웃Panky:stripslashes 不是 real_escape_string 的反义词。这是addslashes()的反面,addslashes对于sql转义是完全没用的。
-
为什么要解决这个问题?你不应该一开始就修复它。使用 PDO 驱动程序和准备好的语句。
标签: php mysql mysql-escape-string