Jump to content

Uploading csv to mySql via php


deeej

Recommended Posts

Hi,

 

Having real trouble for day's with this one, I am trying to upload a csv file to mySql dBase via a php script.

 

I have a csv file and the contents of the file are as follows:

 

Name|250|General|Asset|UK|AM|B1W2|www.website.com|Specialist|1
Name|250|General|Asset|UK|AM|B1W2|www.website.com|Specialist|1
Name|250|General|Asset|UK|AM|B1W2|www.website.com|Specialist|1

 

I have a mysql database set-up with exactly the same amount of columns to accept this data.

 

Here is the PHP script I am using:

 

if(isset($_POST['submit']))

   {

     $filename=$_POST['filename'];

     $handle = fopen("$filename", "r");

     while (($data = fgetcsv($handle, 1000, "|")) !== FALSE)

     {

       $import="INSERT into XXXXX(name,link,issue,date,type,type2,type2,type3,type4,typ45) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]')";

       mysql_query($import) or die(mysql_error());

     }

     fclose($handle);

     print "Import done";



   }

   else

   {



      print "<form action='converter.php' method='post'>";

      print "Type file name to import:<br>";

      print "<input type='file' name='filename' size='20'><br>";

      print "<input type='submit' name='submit' value='submit'></form>";

   }

 

I Keep getting the same error: "Column count doesn't match value count at row 1".

 

Much appreciate any1s help thanks!!

Link to comment
https://forums.phpfreaks.com/topic/101025-uploading-csv-to-mysql-via-php/
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.