一段时间以来,我一直在为一个小问题而苦恼。它已经存在多年,但它只是一个恼人的问题而不是一个严重的问题,我刚刚解决了它。但是现在我想知道是否有人可以帮助我。我进行了一些谷歌搜索,但没有成功。
如果我在这样的 php 文件中从 html 文本区域发布表单:
<form action="http://action.com" method="post">
<textarea name="text"><a href="http://google.com">google's site</a></textarea>
</form>
当然还有提交按钮等等。
值就是问题:<a href="http://google.com">google's site</a>
textarea 的值同时具有“(引号)和'(撇号)。
为了将其保存在 mysql_database 中,我这样做:
$result = mysql_query("INSERT INTO `table` (`row1`) VALUES ('".$_POST['text']."') ") or die(mysql_error());
现在我得到了 mysql 错误:
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 site'' at line 1
最佳答案
您的 sql 字符串将是:
INSERT INTO `table` (`row1`) VALUES ('google's site')
这不是一个有效的陈述。正如 Nanne 所写,至少使用 mysql_real_escape_string 转义字符串:http://php.net/manual/en/function.mysql-real-escape-string.php
并阅读有关 sql 注入(inject)的内容 http://en.wikipedia.org/wiki/SQL_injection
想一想:如果有人发帖:$_POST['text']
with value: ');delete from table;....
你可以和你的数据说再见了:)
始终过滤/转义输入!
编辑:从 PHP 5.5.0 开始,不推荐使用 mysql_real_escape_string 和 mysql 扩展。请改用 mysqli 扩展和 mysqli::escape_string 函数
关于php - Mysql + php 带有特殊字符,如 '(Apostrophe) and "(引号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4659879/