Jump to content

CSV Import


mycow

Recommended Posts

I have some piece of code which works when my csv file has data between " and seperated by ;

But when i export from excel to csv the data is not between " but only seperated by ; and nothing else.

Can anyone help me?

 

The code

$fcontents = file ('./file.csv');

  for($i=2; $i<sizeof($fcontents); $i++) { 
      $line = trim($fcontents[$i]); 
      $arr = explode(";", $line); 
      $sql = "insert into table_name values (". 
                  implode(",", $arr) .")"; 
      mysql_query($sql); 
      if(mysql_error()) { 
         echo mysql_error() ."<br>\n"; 
      }
}

Link to comment
https://forums.phpfreaks.com/topic/135617-csv-import/
Share on other sites

That just means your missing quotes in the SQL.

 

Add some single quotes:

      $sql = "insert into table_name values ('". 
                  implode("','", $arr) ."')"; 

 

Since they not obviously different/hard to see, I added single quotes ' in the implode, and before/after the VALUES() portion of the SQL.

Link to comment
https://forums.phpfreaks.com/topic/135617-csv-import/#findComment-706571
Share on other sites

If what mrdamien suggested don't work for ya, then check out this other post I made a while back. It is a csv upload form that takes the csv file and uses column names to make an associative array out of the data.

 

http://www.phpfreaks.com/forums/index.php/topic,156812.msg681607.html#msg681607

 

You will probably need to modify it a bit, but it works great.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/135617-csv-import/#findComment-706582
Share on other sites

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.