【问题标题】:Storing form data with single quotes to MySQL [duplicate]将带有单引号的表单数据存储到 MySQL [重复]
【发布时间】:2012-09-21 12:40:21
【问题描述】:

可能重复:
PHP, why do you escape my quotes?

如果我在输入字段中输入文本,然后尝试使用 PHP mysqli Prepared Statements 将其显示或保存到 MySQL,我会在单引号之前得到一个额外的反斜杠。

$name = $_POST['name'];
echo "name=($name)";

这输出: 不 我还将这个额外的反斜杠存储在 MySQL 中。

我怎样才能避免这种情况并且仍然能够输入反斜杠?

问候, 马格努斯链

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

使用斜线,

echo stripslashes("Who\'s Kai Jim?"); //Who's Kai Jim?

【讨论】:

    【解决方案2】:

    打开您的 php.ini 文件并转到第 460 行(几乎)并关闭魔术引号。喜欢

    ; Magic quotes for incoming GET/POST/Cookie data.
    magic_quotes_gpc = Off
    
    ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
    magic_quotes_runtime = Off
    

    保持魔术引号关闭是开始应用程序安全性的好方法。魔术引号本质上是坏的。它们旨在清理 PHP 脚本的输入,但在不知道如何使用该输入的情况下,无法正确清理。

    魔术引号的 PHP 手册页同意:

    "This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed."
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 2021-06-18
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      相关资源
      最近更新 更多