Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update query with a string in it....

Status
Not open for further replies.

karnaf

Programmer
Nov 16, 2002
41
IL
Hi,

I'm using MySQL with php3 and I encountered a problem when trying to update the DB using an update string like this -

$update_sql_str = "UPDATE table SET field='$string' WHERE id = $id ";

Now, the problem is that $string is a variable containing a string that has both 's and "s in it so I keep getting "You have an error in your SQL syntax near..." since the SQL query thinks the data for field ends before it really does.

For example, if $string = Joe said "it's a beatiful day"
then if I use the above syntax, it thinks that field is only [Joe said "it] and if I use SET string="$string", it thinks that field is only [Joe said ].

Is there another syntax I can use?

Thanks.
 
$string=addslashes($string); // add a \ before each ' or " that causes you problems, mysql should then understand how to deal with your insert

$update_sql_str = "UPDATE table SET field='$string' WHERE id = $id ";
______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks guys.

the addslashes() function did the job :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top