tryingtolearn Posted May 18, 2008 Share Posted May 18, 2008 Hi again all, I am trying to upload a csv file and insert the data into a DB But when I encounter an apostrophe in the data it stops Is there a way around this? Code Im using require_once ('../mysql_connect.php'); // Connect to the database. $filename = $path.$date.$HTTP_POST_FILES['userfile']['name']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT into leads (prfname2,prlname2,prcaddr12,prcaddr22,prccity2,prcstate2,prcpostco2,prevntdate,prcreated,prhomepho2,premail2,add1,add2,add3,add4) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','Y','2','3','4')"; mysql_query($import) or die(mysql_error()); } fclose($handle); For example - everything goes good and then say an address field in the csv file is 555 ALEXANDER'S OAK It stops right there w/ an error. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/106151-solved-upload-a-csv-file-insert-into-db-stops-when-it-encounters-an-apostrophe/ Share on other sites More sharing options...
phpzone Posted May 18, 2008 Share Posted May 18, 2008 You need to use mysql_real_escape_string with your $data variables to escape the apostrophes eg. mysql_real_escape_string( $data[0] ) Quote Link to comment https://forums.phpfreaks.com/topic/106151-solved-upload-a-csv-file-insert-into-db-stops-when-it-encounters-an-apostrophe/#findComment-544087 Share on other sites More sharing options...
tryingtolearn Posted May 18, 2008 Author Share Posted May 18, 2008 Thanks for the fast reply phpzone Unfortunetly that didnt work I changed the code to look like this require_once ('../mysql_connect.php'); // Connect to the database. $filename = $path.$date.$HTTP_POST_FILES['userfile']['name']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT into leads (prfname2,prlname2,prcaddr12,prcaddr22,prccity2,prcstate2,prcpostco2,prevntdate,prcreated,prhomepho2,premail2,add1,add2,add3,add4) values('mysql_real_escape_string($data[0])','mysql_real_escape_string($data[1])','mysql_real_escape_string($data[2])','mysql_real_escape_string($data[3])','mysql_real_escape_string($data[4])','mysql_real_escape_string($data[5])','mysql_real_escape_string($data[6])','mysql_real_escape_string($data[7])','mysql_real_escape_string($data[8])','mysql_real_escape_string($data[9])','mysql_real_escape_string($data[10])','Y','2','3','4')"; mysql_query($import) or die(mysql_error()); //print_r($data); } fclose($handle); But I still get this 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 'S OAK)','mysql_real_escape_string()','mysql_real_escape_string(CINCINNATI)','mys' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/106151-solved-upload-a-csv-file-insert-into-db-stops-when-it-encounters-an-apostrophe/#findComment-544093 Share on other sites More sharing options...
tryingtolearn Posted May 18, 2008 Author Share Posted May 18, 2008 Nevermind, got it working - had a typo - thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/106151-solved-upload-a-csv-file-insert-into-db-stops-when-it-encounters-an-apostrophe/#findComment-544097 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.