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

Member Avatar for diafol

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.