【发布时间】: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
提前感谢您的帮助!
【问题讨论】:
-
如果您使用准备好的参数化语句,这应该不是问题。您的脚本存在SQL Injection Attack 的风险。看看Little Bobby Tables 发生了什么。甚至 if you are escaping inputs, its not safe! 使用 prepared parameterised statements 代替。
-
@TomUdding 你能举个例子说明我需要做什么吗?因为我一个字都听不懂你在说什么……
-
htmlentities()出去,而不是进来。你应该使用准备好的语句——你使用哪个 API 来查询数据库?mysql_*?mysqli_*? PDO?