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

Link to comment
Share on other sites

;D  :o

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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