Mysql Real Escape String PHP函数在我的字段输入中添加"\"

How*_*opa 5 php mysql magic-quotes

我正在使用PHP向MySQL数据库提交表单.

我通过该mysql_real_escape_string($content)函数发送表单数据.

当条目出现在我的数据库中(在phpMyAdmin中检查)时,我的所有双引号和单引号都被转义.

我很确定这是一个PHP配置问题?

所以:

$content = 'Hi, my name is Jascha and my "favorite" thing to do is sleep';
mysql_real_escape_string($content);
$query = 'INSERT INTO DB...'
Run Code Online (Sandbox Code Playgroud)

在我的数据库中出现:

嗨,我的名字是Jascha,我最喜欢的事情就是睡觉

我该告诉谁该怎么办?(我无法访问php.ini).

Pas*_*TIN 6

如果您从表单中获取$ content数据(而不是PHP代码中的"原样"),可能因为Magic引用而遇到问题(请参阅参考资料)magic_quotes_gpc

基本上:

当magic_quotes打开时,所有' (单引号),"(双引号),\ (反斜杠)和NUL都会自动转义为反斜杠

如果启用了魔术引号(例如,您可以在输出中检查phpinfo()),那么您将获得那种"双重转义":

  • 这些角色将通过魔术引号转义一次,
  • 然后,他们将第二次逃脱 mysql_real_escape_string


在这种情况下,好的解决方案不是停止使用mysql_real_escape_string,而是在配置中禁用magic_quotes_gpc ...

......但是,你没有访问它,你实际上必须"恢复"魔术引号的作用,呼吁stripslashes在你要作为输入$_GET,并$_POST使用它之前begining.

注意:这是在(引用)手册页上给出的建议:mysql_real_escape_string

注意:如果magic_quotes_gpc启用,则首先应用于 stripslashes()数据.对已经转义的数据使用此函数将两次转义数据.


Bal*_*usC 5

在检索请求数据时,您需要考虑魔术引号.如果get_magic_quotes_gpc()true,那么你需要运行stripslashes()输入.最好的方法是为此编写一个函数.就像是:

function get_string($array, $index, $default = null) {
    if (isset($array[$index]) && strlen($value = trim($array[$index])) > 0) {
        return get_magic_quotes_gpc() ? stripslashes($value) : $value;
    } else {
        return $default;
    }
}
Run Code Online (Sandbox Code Playgroud)

..你可以用作

$input = get_string($_POST, 'input');
Run Code Online (Sandbox Code Playgroud)

..代替

$input = $_POST['input'];
Run Code Online (Sandbox Code Playgroud)

做同样的琐碎的东西一样get_number(),get_boolean(),get_array()等等.