apostrophes are breaking my mysql query in PHP

青春壹個敷衍的年華 提交于 2019-11-26 23:22:59

问题


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.


回答1:


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.




回答2:


Have a look here

  • How can I use an apostrophe (') in a query string?
  • mysql_real_escape_string
  • PHP mysql_real_escape_string


来源:https://stackoverflow.com/questions/1961308/apostrophes-are-breaking-my-mysql-query-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!