Page 1 of 1

[Solved]when variable contains a quot Insert does not work

Posted: Tue Aug 22, 2006 3:42 am
by byenary
$qs = "INSERT INTO videodb VALUES ('$id','$title','$artist');";
$result = mysql_query($qs);

This code normaly works unless $artist or $title contains a quot '
I when i echo $qs and try it in phpmyadmin, it says quot not closed and refering to the end...
Any way to work around this issue ?

Byenary

Posted: Tue Aug 22, 2006 4:07 am
by CoderGoblin
You need to add a backslash immediately before the quote..

Code: Select all

$qs="SELECT 'This isn't working'"
will not work

Code: Select all

$qs="SELECT 'This isn\'t working'"
will.

For user/variable entered text you may want to use addslashes

Posted: Tue Aug 22, 2006 4:11 am
by byenary
Thx addslashes does the job !

Posted: Tue Aug 22, 2006 4:18 am
by volka
Please use mysql_real_escape_string instead of addslashes.

Posted: Tue Aug 22, 2006 5:17 am
by CoderGoblin
Never noticed that one, following on from that (I hardly ever use Mysql) there is a similar command for postgres which I have just found...
pg-escape-string