0

I am using the followng code, to import csv into mysql.

 if($_POST['sform'] == "1")
{
     $fname = $_FILES['upload']['name'];        
     $chk_ext = explode(".",$fname);        
     if(strtolower($chk_ext[1]) == "csv")
     { 
        $filename = $_FILES['upload']['tmp_name'];
        $row = 1;
        if (($handle = fopen($filename, "r")) !== FALSE)
        {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
              {
                $row++;
                $data_entries[] = $data ;
              }
              fclose($handle);
         }
         foreach($data_entries as $line)
         {
              $sql = "INSERT into testposts(ID,UID,title,tags,desc) values('','$data[0]','$data[1]','$data[2]','$data[3]')";
$conn->execute($sql);
          $conn->execute($line);
         }
         $msg = "Successfully Imported";
     }
     else
     {
         $error = "Invalid File";
     }   
}

When I try to update then all values gets updated as 0

my csv file as follow (example)

1,title,tags,send2

Am not including ID value as it is auto incremented

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by sammry
0

SHouldn't

$sql = "INSERT into testposts(ID,UID,title,tags,desc) values('','$data[0]','$data[1]','$data[2]','$data[3]')";

be

$sql = "INSERT into testposts(ID,UID,title,tags,desc) values('','$line[0]','$line[1]','$line[2]','$line3]')";

ANd what's this for:

 $conn->execute($line);

Also, you could build up aan sql statement in the loop and using the VALUES syntax just send one query to the DB server as opposed to a shedfull

Edited by diafol

0

thats a great help, i just played with looping, it all worked good, thanks so much for your help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.