0

我目前遇到这个问题,我使用 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 问题,大多数人声称这不应该发生,是否有解决方法?

4

1 回答 1

0

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

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

magic_quotes_runtime当从数据库中检索数据时,还有一个转义数据。这也应该关闭。

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

于 2014-08-21T17:38:16.073 回答