问题
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