Jump to content

[SOLVED] import csv data into mysql


yanti

Recommended Posts

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>";
   }
   

???  ???  :-\

Link to comment
https://forums.phpfreaks.com/topic/158066-solved-import-csv-data-into-mysql/
Share on other sites

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  :)

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')";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.