我正在使用 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 applystripslashes()
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/