Hi I am trying to import a bunch of values from a txt file, separated by the | Pipe, but I keep getting the error "Column count doesn't match value count at row 1", so some help would be appreciated. 🙂

The data is in the following format:
ProductID|Name|MerchantID|Merchant|Link|Thumbnail|BigImage|Price|RetailPrice|Category|SubCategory|Description|Custom1|Custom2|Custom3|Custom4|Custom5|LastUpdated

The database has the same structure (just renamed a few fields).

The PHP code is as follows. 🙂

<?php

include "connect.php";

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

   {

 $filename=$_POST['filename'];

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

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

 {



   $import="INSERT into products(PID,pName,mID,pMerchant,pLink,pThumb,pImage,pPrice,pRetailPrice,pMainCat,pSubCat,pDescription,pCustom1,pCustom2,pCustom3,pCustom4,pCustom5,LastUpdate) values('$data[0]'|'$data[1]'|'$data[2]'|'$data[3]'|'$data[4]'|'$data[5]'|'$data[6]'|'$data[7]'|'$data[8]'|'$data[9]'|'$data[10]'|'$data[11]'|'$data[12]'|'$data[13]'|'$data[14]'|'$data[15]'|'$data[16]'|'$data[17]')";

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

   }
?>

    The issue is that you're using the | (pipe) character instead of the , (comma) character in your SQL statement. change all "|" to "," in the SQL statement and you should be fine.

    $import="INSERT into products(PID,pName,mID,pMerchant,pLink,pThumb,pImage,pPrice,pRetailPrice,pMainCat,pSubCat,pDescription,pCustom1,pCustom2,pCustom3,pCustom4,pCustom5,LastUpdate) values('$data[0]', '$data[1]', '$data[2]', '$data[3]', $data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]', '$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]', '$data[15]', '$data[16]', '$data[17]')";
    

      Now I am getting a weird problem.

      When I import the file it adds the same data to the database 12 times, and I get this error:

      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mega' in the 'flash' with the Mega Flash DMX, a powerful DMX-compatible strobe l' at line 1

        Sweet got it working. 🙂

        Just had to do two things:
        a) Remove a few of the first records (not sure why exactly)
        b) Remove all the apostrophes

        🙂

          Glad to hear you got it sorted. Don't forget to mark this thread as resolved.

            daverules wrote:

            b) Remove all the apostrophes

            Or you could have just sanitized the data using a function such as [man]mysql_real_escape_string/man (which you should be doing anyway).

              Write a Reply...