Jump to content

[SOLVED] apostrophe is hosing my mysql insertion


86Stang

Recommended Posts

I'm pretty new to alot of this so bear with me.

 

I am importing data into my mysql db as follows:

 

$import="INSERT into classifieds(cat_name,description) values('$name','$ad')";

mysql_query($import) or die(mysql_error());

 

It works fine until it gets to a row that has an apostrophe in the description at which point it kicks out

 

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 's Heating is looking for exp. installers, trainees, and gas pipers in residentia' at line 1

 

Any ideas what I can do short of replacing the apostrophies?

Link to comment
Share on other sites

Yep.  You need to escape the strings (indented for legibility):

 

$import=sprintf("INSERT into classifieds(cat_name,description) values('%s','%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($ad)
);

 

This also has the benefit of making your queries less suseptible to injection-type mischief.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.