【问题标题】:Insert values containing special characters into MySQL database将包含特殊字符的值插入 MySQL 数据库
【发布时间】:2014-05-27 07:21:26
【问题描述】:

我目前正在尝试使用 TinyMCE 设置评论系统,它可以使用普通字符和 PHP 标记等。但是当我用这个ed o'neill 发表评论时,它只会在我的数据库中插入一个空行。

我的表单输入中有htmlspecialcharsmysqli_real_escape_string

如何修复空行插入?

$post_content = $_POST['post_content'];
                        //$post_content = htmlspecialchars($post_content);
                        //$post_content = mysqli_real_escape_string($post_content);

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 它的 2014 年,真的更好地使用现代库来使用 Mysql 或查看@PDO
  • 有代码,大概 10 秒钟,然后就消失了。
  • Always use contextual escaping.htmlspecialchars 与这里无关——它是为了防止 XSS 攻击。您只需要单独转义特殊字符。为此,请使用 m_r_e_s() 单独 或使用准备好的语句(首选选项)。
  • 我仍然是 php 的“新手”,准备好的语句是什么意思?喜欢面向对象?或

标签: php mysql


【解决方案1】:

看看准备好的语句,它会为你做所有的转义

编辑:Here's the link to the PHP manual, courtesy of Fred -ii-

【讨论】:

  • 你的意思是like this?
  • 就是这个意思
  • ,我是这么想的 ;-)
【解决方案2】:

您已经注释了一些代码,但mysqli_real_escape_string 需要 2 个参数,请参阅here 了解更多信息

string mysqli_real_escape_string ( mysqli $link , string $escapestr );

你的代码应该是:

$post_content = mysqli_real_escape_string($connection,$post_content); //$connection should be your database connection string

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2012-03-24
    • 2018-02-26
    • 2016-02-18
    • 2021-03-27
    • 2011-02-04
    • 1970-01-01
    • 2012-11-28
    相关资源
    最近更新 更多