Forum Moderators: coopster
Whenever I try to post an ad that contains the character ' in the title I get an error.
So for example if the title of my ad is: 12' pencil
I get the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pencil','nick','32','accessories','2007-10-16','Deonne','2007-12-15
I don't understand because the 2 fields are both varchar 255 but if I use the ' symbol in the adInfo fields, I get no error. Only in adTitle.
Anyone knows what's wrong?
Thanks
12\' pencil
Will mysql_real_escape_string not add the ' character to the database? If that is the case I do not want that. People that post ad use ' as the abbreviation for inches. So is there anothe way around this?
so if you have:
mysql_query("Insert into database (title) values ('He's a good man')");
only 'He' will be read by mysql and there rest will cause an error.
In PHP (and mysql) you have to escape the (') character.
print 'He's a good man'; won't work, you must do
print 'He\'s a good man';
which will out put correctly
He's a good man
[edited by: CWebguy at 7:54 pm (utc) on Oct. 16, 2007]
$sql = "INSERT INTO ad(adName,adPrice) VALUES ('".mysql_real_escape_string($adName)."','".mysql_real_escape_string
($adPrice)."')";
$adName is the one with the problem.
Although you are only having issues with that string, all user-defined strings in queries must be escaped; that is why I escaped both strings and not just the one.