【问题标题】:Can't insert texts with ' symbol in database using php [duplicate]无法使用php在数据库中插入带有'符号的文本[重复]
【发布时间】:2017-11-09 04:01:09
【问题描述】:

所以我遇到了这个问题,我似乎无法将包含以下字符的文本插入到我的数据库中:'(例如:It's)。我以前遇到过这个问题,我只是去掉了符号,但现在我真的需要一个解决方案,在整个网络上无处或堆栈溢出我似乎无法找到一个体面和明确的答案。

这是我的数据的显示方式:

HTML

<textarea name="text_from_form">
      This is the kind of stuff I need and It's awesome (user input example)
</textarea>

PHP

<?php

  $my_text = $_POST["text_from_form"};
  $my_text_new = htmlentities($my_text);

  $sql = "INSERT INTO tableName (text)
  VALUES ('$my_text_new')";
?>

错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's awesome ')' at line 2

提前感谢您的帮助!

【问题讨论】:

标签: php mysql insert


【解决方案1】:

htmlentities() 函数不会在默认标志 ​​(ENT_COMPACT) 中转义单引号 ',使用 htmlentities($text, ENT_QUOTES) 或函数 mysql_real_escape_string()mysqli_real_escape_string(),具体取决于您使用的库。

【讨论】:

    猜你喜欢
    • 2016-12-13
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2016-02-25
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多