86Stang Posted May 1, 2007 Share Posted May 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/49530-solved-apostrophe-is-hosing-my-mysql-insertion/ Share on other sites More sharing options...
paul2463 Posted May 1, 2007 Share Posted May 1, 2007 try this $amendName = addslashes($name); $amendAd = addslashes($ad); $import="INSERT into classifieds(cat_name,description) values('$amendName','$amendAd')"; mysql_query($import) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/49530-solved-apostrophe-is-hosing-my-mysql-insertion/#findComment-242766 Share on other sites More sharing options...
Wildbug Posted May 1, 2007 Share Posted May 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49530-solved-apostrophe-is-hosing-my-mysql-insertion/#findComment-242768 Share on other sites More sharing options...
86Stang Posted May 1, 2007 Author Share Posted May 1, 2007 Worked like a charm, thanks guys!! Quote Link to comment https://forums.phpfreaks.com/topic/49530-solved-apostrophe-is-hosing-my-mysql-insertion/#findComment-242782 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.