stfc_boy
09-03-2009, 04:35 PM
Hi All,
I'm trying to write a parser in php which takes a simple tab-delimited file and uploads it to a table in mysql. Here's what I effectively want to do:
1) Take the tab delimited file
http://www.inspireaway.co.uk/td/parse.jpg
2] And upload the data row by row in the table.jpg (note the other column is set in the below code as you'll see)
http://www.inspireaway.co.uk/td/table.jpg
Now, here's my code for trying to upload this data row-by-row:
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$data = file ($tmpName);
foreach ( $data as $line ) {
$val = trim ($line);
$arr = explode ("\t", $val);
$val = "'" . implode ("','", $arr) . "'";
$query = "INSERT INTO tab_del (colour,other) VALUES (".$val.",'sometext')";
mysql_query($query) or die(mysql_error());
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
echo "<br>File $fileName uploaded<br>";
}
?>
<h2>Tab testing</h2>
<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>
But when I try and upload the data I get the message Column count doesn't match value count at row 1
Can anyone help?
Thanks
I'm trying to write a parser in php which takes a simple tab-delimited file and uploads it to a table in mysql. Here's what I effectively want to do:
1) Take the tab delimited file
http://www.inspireaway.co.uk/td/parse.jpg
2] And upload the data row by row in the table.jpg (note the other column is set in the below code as you'll see)
http://www.inspireaway.co.uk/td/table.jpg
Now, here's my code for trying to upload this data row-by-row:
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$data = file ($tmpName);
foreach ( $data as $line ) {
$val = trim ($line);
$arr = explode ("\t", $val);
$val = "'" . implode ("','", $arr) . "'";
$query = "INSERT INTO tab_del (colour,other) VALUES (".$val.",'sometext')";
mysql_query($query) or die(mysql_error());
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
echo "<br>File $fileName uploaded<br>";
}
?>
<h2>Tab testing</h2>
<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>
But when I try and upload the data I get the message Column count doesn't match value count at row 1
Can anyone help?
Thanks