php - Mysql Real Escape String PHP函数添加 "\"到我的字段条目

标签 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...'

在我的数据库中显示为:

嗨,我叫 Jascha,我“最喜欢”做的事情是 sleep

我该告诉谁该做什么? (我无法访问 php.ini)。

最佳答案

如果您从表单(而不是 PHP 代码中的“原样”)获取您的 $content 数据,也许您遇到问题是因为魔术引号 (参见 magic_quotes_gpc )

基本上:

When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically

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

  • 这些字符将被魔术引号转义一次,
  • 然后,它们将被 mysql_real_escape_string 第二次转义


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

...但是,由于您无权访问它,您实际上必须“恢复”魔术引号的效果,调用 stripslashes在开始使用之前,您将获得 $_GET$_POST 的输入。

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

Note: If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function on data which has already been escaped will escape the data twice.

关于php - Mysql Real Escape String PHP函数添加 "\"到我的字段条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2284099/

相关文章:

php - 使用 apache 和 windows 为 Backbone.js 设置 RESTful 服务

php - 如何将数组值插入数据库表的列?

PHP,SQL多表

mysql - 如何选择给定日期的最后一个条目

PHP 魔术引号问题

php - 为什么 php 不能将引号转换为 mysql 的 html 实体?

javascript - 单击按钮设置单选按钮在基于值的模态 Bootstrap 上单击

php - 网站如何在不重新加载页面的情况下检查登录凭据?

php - 发布动态输入表单并获取数组

php - 如何在运行时禁用 PHP 魔术引号?