撇号破坏了我在 PHP 中的 mysql 查询
我的数据库的名称记录偶尔包含撇号,例如 Joe's Bar
,我刚刚用 PHP 编写了一个查询脚本,该脚本获取该字段并将其粘贴到使用通常的 $ 的 select 语句中query = "SELECT address FROMrestaurant WHERE name='$name'";
并且某些餐厅名称中的撇号使爱情列车脱轨。
我该如何防止这种情况发生?
Snide 答案 - 使用与通过 PHP 将它们插入数据库时使用的相同技术。
反驳 - 当时我也遇到了同样的问题,我作弊并直接使用 PHPMyAdmin 输入了麻烦的问题,但这不能再被忽视了。
感谢您在假期期间抽出时间来回答这个问题。
My database has name records that occasionally contain apostrophes, such as Joe's Bar
and I've just coded a query script in PHP that grabs that field and sticks it into a select statement with the usual $query = "SELECT address FROM restaurants WHERE name='$name'";
and the apostrophe in some of the restaurant names derails the Love Train.
How do I keep this from happening?
Snide answer - Use the same technique you used when you inserted them INTO the database via PHP.
Rebuttal - I was having the same problem then and cheated and entered the troublesome ones directly using PHPMyAdmin but this can't be ignored any longer.
Thank you for taking the time to answer this during the holidays.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在该行之前
$name = mysql_real_escape_string($name);
。您可能还想阅读 SQL 注入,因为您的输入显然未经过滤。
You have to
$name = mysql_real_escape_string($name);
before that line.You might also want to read up on SQL Injections, since your inputs are clearly unsanitized.
看看这里
查询字符串?
Have a look here
query string?