Hi Guys,
Trying to load a tab-delimited file to a MySql database as per this thread with a few changes, but getting the error message:
INSERT INTO test (first_name, last_name, department, dob) VALUES ('')Column count doesn't match value count at row 1
But I have the same amount of columns in my table as I do in my tab-delimited file being uploaded. I've also echoed out the query, but still don't understand. Can anyone please advise by looking at the code below – especially my INSERT into statement?
Many Thanks
Chris
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
for($i=0; $i<sizeof($fp); $i++)
{
$line = trim($fp[$i]);
$arr = explode("\t", $line);
$query = "INSERT INTO test (first_name, last_name, department, dob) ".
"VALUES ('". implode("','", $arr) ."')";
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$hostname_mydba = "localhost";
$database_mydba = "********";
$username_mydba = "*********";
$password_mydba = "***********";
$mydba = mysql_connect($hostname_mydba, $username_mydba, $password_mydba) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_mydba);
}
fclose($fp);
echo $query
mysql_query($query) or die(mysql_error());
echo "<br>File $fileName uploaded<br>";
}
?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</td>
</tr>
</table>
</form>