yanti Posted May 14, 2009 Share Posted May 14, 2009 hi guys...i hv this code which import csv data into mysql....but a little bit problem.....i got an error during runtime saying that MySql syntax error which i cant fix it....hope anyone can help me.... for ur information i got more than 40000 rows of data, but it can insert only 9 rows of it in database then error appear on webpage....] if(isset($_POST['submit'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { $import="INSERT into kmmb_member1(no_ahli,no_pin,nama,no_ic_baru,no_ic_lama) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='import.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } ??? ??? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/ Share on other sites More sharing options...
deepson2 Posted May 14, 2009 Share Posted May 14, 2009 Try this code: if ((isset($HTTP_POST_VARS["MM_update"])) && ($HTTP_POST_VARS["MM_update"] == "form1")) { //form1 is form name // yourfile.csv must be in the same directory as this php file $handle = fopen ("Book1.csv","r"); //this ll be your file name while ($data = fgetcsv ($handle, 100000, ",")) { $no_ahli = $data[0]; $no_pin = $data[1]; $nama = $data[2]; $no_ic_baru = $data[3]; $no_ic_lama = $data[4]; $import="INSERT INTO p_b (no_ahli,no_pin,nama,no_ic_baru,no_ic_lama) values ('".$no_ahli."','".$no_pin."','".$nama."','".$no_ic_baru."','".$no_ic_lama."')"; check this code and let us know. what happens Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833883 Share on other sites More sharing options...
Mchl Posted May 14, 2009 Share Posted May 14, 2009 Telling us what the actual error message was would also help. Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833885 Share on other sites More sharing options...
yanti Posted May 14, 2009 Author Share Posted May 14, 2009 the error is like this... 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 '','','02159002')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833887 Share on other sites More sharing options...
yanti Posted May 14, 2009 Author Share Posted May 14, 2009 HI deepson2... i tried ur code...it doesnt give any error...but still not insert all rows in csv data. it just entered until row 9.... Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833890 Share on other sites More sharing options...
yanti Posted May 14, 2009 Author Share Posted May 14, 2009 hey guys, now i know why i got the error like that..... because in my csv data there is a value using like this....(Dato') that's why the error appears...coz mysql cant detect it.... am i correct? i think so.... Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833894 Share on other sites More sharing options...
Mchl Posted May 14, 2009 Share Posted May 14, 2009 Run mysql_real_escape_string on values to be inserted into query. Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833899 Share on other sites More sharing options...
yanti Posted May 14, 2009 Author Share Posted May 14, 2009 hi Mchl... sorry, but where to put that line of code? can u please show me? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833908 Share on other sites More sharing options...
Mchl Posted May 14, 2009 Share Posted May 14, 2009 Something like this: $no_ahli = mysql_real_escape_string($data[0]); $no_pin = mysql_real_escape_string($data[1]); $nama = mysql_real_escape_string($data[2]); $no_ic_baru = mysql_real_escape_string($data[3]); $no_ic_lama = mysql_real_escape_string($data[4]); $import="INSERT INTO kmmb_member1 (no_ahli,no_pin,nama,no_ic_baru,no_ic_lama) values ('$no_ahli','$no_pin','$nama','$no_ic_baru','$no_ic_lama')"; Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-833913 Share on other sites More sharing options...
yanti Posted May 15, 2009 Author Share Posted May 15, 2009 hey..thanks everyone...finally...it solved.... Quote Link to comment https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/#findComment-834502 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.