flemingmike Posted September 30, 2011 Share Posted September 30, 2011 hi, i have a script that inserts a csv into mysql. i run into errors when there is an empty field example: filed1,field2,field3,,field5,,field7 here is my code that processes it. $handle = fopen("uploads/$filename", "r"); while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import .= "INSERT into goaliestats(id,name,gp,w,so,gaa,date) values(NULL,'$data[0]','$data[2]','$data[8]','$data[26]','$data[18]','$data[28]')" . "\n"; mysql_query($import) or die(mysql_error()); } fclose($handle); is there a way i can make a ,, into ,0, ? thanks! Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/ Share on other sites More sharing options...
flemingmike Posted September 30, 2011 Author Share Posted September 30, 2011 nevermind. this isnt my problem. ime getting an error Notice: Undefined variable: import in E:\Website\hockeypool\admin\import.php on line 31 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 'INSERT into goaliestats(id,name,gp,w,so,gaa,date) values(NULL,'Carey Price','72'' at line 2 this is the line thats having the error: Carey Price,VAN,72,,60,,3590,,38,,15,,7,,3,,126,,2.11,,1753,,1627,,0.928,,4,,9/29/2011 any ideas on this? Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274371 Share on other sites More sharing options...
Buddski Posted September 30, 2011 Share Posted September 30, 2011 You are trying to append the SQL string to the $import variable that isnt defined. Remove the fullstop before your = sign and your error should be fixed. $import = "INSERT into goaliestats(id,name,gp,w,so,gaa,date) values(NULL,'$data[0]','$data[2]','$data[8]','$data[26]','$data[18]','$data[28]')" . "\n"; Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274373 Share on other sites More sharing options...
Muddy_Funster Posted September 30, 2011 Share Posted September 30, 2011 oh, and you should stop inserting NULL into an auto_inc field as well. Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274377 Share on other sites More sharing options...
flemingmike Posted September 30, 2011 Author Share Posted September 30, 2011 thank you! i just found one other problem. one of the fields is Matt D'Agostini. this is giving me an error due to the ' is there any way i can allow this? Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274379 Share on other sites More sharing options...
Buddski Posted September 30, 2011 Share Posted September 30, 2011 Everything being entered into the database should be escaped. mysql_real_escape_string() This will also help protect you from SQL Injection. Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274380 Share on other sites More sharing options...
flemingmike Posted September 30, 2011 Author Share Posted September 30, 2011 thanks everyone! Link to comment https://forums.phpfreaks.com/topic/248175-str-replace-double-comma/#findComment-1274383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.