PDA

View Full Version : quotes in mysql query fields



stevenvh
08-19-2009, 10:25 AM
Hi,
I want to write data from a textfile to a mysql table. The following code works for the first few records, but then fails.

$query = "INSERT INTO mytable (id, title, page ) VALUES ('$i', '$title', '$page')";
mysql_query($query) or die('Error, query failed');

It appears that in the 'title' field of the incriminating record there's a single quote, which I presume to be the culprit. How can I make the query swallow the sql statement?
I guess double quotes will be no good, as they also appear in the record.

TIA
Steven

stevenvh
08-19-2009, 11:47 AM
Don't bother, I found that simply escaping does the trick:

$title = str_replace("'", "\'", $title);

felgall
08-19-2009, 01:13 PM
You should be doing it properly using mysql_real_escape_string() to convert all the problem characters in fields before inserting them.