【问题标题】:Reverse mysqlescapestring [duplicate]反向mysqlescapestring [重复]
【发布时间】: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 that magic_quotes_gpc is disabled
  • @MichaelBerkowski 问题已更新
  • 当数据输入数据库时​​,您通过 real_escape_string 添加的任何转义都将消失。如果你得到了额外的转义,那么有些东西已经为你转义了你的文本,你实际上是在双重转义。
  • @Hanky웃Panky:stripslashes 不是 real_escape_string 的反义词。这是addslashes()的反面,addslashes对于sql转义是完全没用的。
  • 为什么要解决这个问题?你不应该一开始就修复它。使用 PDO 驱动程序和准备好的语句。

标签: php mysql mysql-escape-string


【解决方案1】:

我假设这些值来自$_POST。在这种情况下,您在php.ini 中有magic_quotes_gpc = true。这会转义通过 POST、GET 和 COOKIE 发送的数据。将其关闭 (false),或在全局包含/头文件中执行:

$_POST = array_map('stripslashes', $_POST);

还有magic_quotes_runtime,它在从数据库中检索数据时会转义数据。这也应该关闭。

如果您使用准备好的语句,您不必担心手动转义,但magic_quote 指令仍然需要关闭。

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2015-08-02
    • 2011-05-06
    • 2017-09-12
    • 1970-01-01
    相关资源
    最近更新 更多