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.
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