Hi,
I got this bit of code to upload a csv file but i get this error and im not sure what it is, im sure its a simple thing but i cant see it.
Query failed: Column count doesn't match value count at row 1
Any ideas?
<?php
require_once('./Includes/host.php');
?>
<?php
//First we need to define our user pass and database name
$tbl = "test";
//$host="yourdatabasehost";
//$user="youruser";
//$pass="yourpass";
//$database="yourdatabase";
//we need function to get the csv
function getcsv($filename, $delim =","){
$row = 0;
$dump = array(); //create new array for hold the data csv
$f = fopen ($filename,"r");
$size = filesize($filename)+1;
while ($data = fgetcsv($f, $size, $delim)) {
$dump[$row] = $data; //put the data to array
//echo $data[1]."<br>";
$row++;
}
fclose ($f);
echo "hereget";
return $dump;
}
//this function for insert data to csv
function makeINSERTS($text, $table){
global $linkdata; //make global database connection
$insert = array(); //make array for hold data insert
$i = 0;
while (list($key, $val) = @each($text)){
//insert the data
$insert[$i] = "INSERT into ".$table." VALUES('','";
$insert[$i] .= implode("','", $val);
$insert[$i] .= "')\n";
$result = mysql_query($insert[$i], $linkdata) or die('Query failed: ' . mysql_error());
$i++;
}
echo "heremake";
return $insert;
}
//this verify is the file csv upload
if ($_POST["submit"]=="submit") {
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$file=$_FILES['userfile']['tmp_name'];
$linkdata = mysql_connect($hostname_localhost, $username_localhost, $password_localhost) or die('Could not connect: ' . mysql_error());
//$host, $user, $pass)
mysql_select_db($database_localhost, $localhost) or die('Could not select database');
$CSVarray = getcsv($file);
$CSVarray = makeINSERTS($CSVarray, "$tbl");
} else {
echo "error upload file";
exit;
}
} else {
//form upload
echo "
<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=POST>
Upload this file: <INPUT NAME=\"userfile\" TYPE=\"file\">
<INPUT TYPE=\"submit\" VALUE=\"submit\" name=submit></FORM>
";
}
?>
Thanks for any suggestions.