Forum Moderators: coopster
I am trying to upload data from a XMLSpreadsheet to a MYSQl database. I am trying first to display the data so then I can try to figure out how to insert the display data to my table. The thing is that I keep getting an error on line 17 of my code and I've been trying to figure out the problem but still I can't.
If there is anyone that can help me with this or if anyone knows a better way to this please I need help I am getting crazy with this. Any help will be very,very appreciated.
Thanks.
Here is the code for the upload:(simple one)
<?php //PHP ADODB document - made with PHAkt 2.8.2?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form
action="import2.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table width="600">
<tr>
<td>Names file:</td>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
Here is the code for the display after upload:
<?php //PHP ADODB document - made with PHAkt 2.8.2?>
<?php
$data = array();
function add_person( $first, $middle, $last, $email )
{
global $data;
$data []= array(
'first' => $first,
'middle' => $middle,
'last' => $last,
'email' => $email
);
}
//Below is where the error is line 17
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
if (!$first_row )
{
$first = "";
$middle = "";
$last = "";
$email = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind!= null ) $index = $ind;
if ( $index == 1 ) $first = $cell->nodeValue;
if ( $index == 2 ) $middle = $cell->nodeValue;
if ( $index == 3 ) $last = $cell->nodeValue;
if ( $index == 4 ) $email = $cell->nodeValue;
$index += 1;
}
add_person( $first, $middle, $last, $email );
}
$first_row = false;
}
}
?>
<html>
<body>
<table width="449">
<tr>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Email</th>
</tr>
<?php foreach( $data as $row ) {?>
<tr>
<td><?php echo( $row['first'] );?></td>
<td><?php echo( $row['middle'] );?></td>
<td><?php echo( $row['last'] );?></td>
<td><?php echo( $row['email'] );?></td>
</tr>
<?php }?>
</table>
<p> </p>
<p> </p>
</body>
</html>
------------------
This is how I got it:
<?php
echo '<pre>';
print_r($_FILES);
echo '</pre>';
die();
$data = array();
function add_person( $first, $middle, $last, $email )
{
global $data;
$data []= array(
'first' => $first,
'middle' => $middle,
'last' => $last,
'email' => $email
);
}
//Below is where the error is line 17
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
if (!$first_row )
{
$first = "";
$middle = "";
$last = "";
$email = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind!= null ) $index = $ind;
if ( $index == 1 ) $first = $cell->nodeValue;
if ( $index == 2 ) $middle = $cell->nodeValue;
if ( $index == 3 ) $last = $cell->nodeValue;
if ( $index == 4 ) $email = $cell->nodeValue;
$index += 1;
}
add_person( $first, $middle, $last, $email );
}
$first_row = false;
}
}
?>
<html>
<body>
<table width="449">
<tr>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Email</th>
</tr>
<?php foreach( $data as $row ) {?>
<tr>
<td><?php echo( $row['first'] );?></td>
<td><?php echo( $row['middle'] );?></td>
<td><?php echo( $row['last'] );?></td>
<td><?php echo( $row['email'] );?></td>
</tr>
<?php }?>
</table>
<p> </p>
<p> </p>
</body>
</html>
<input type="file" name="file" />
in your form element try to change the name from file to something else, not sure how much it will matter
<input type="file" name="userfile" />
you'll have to change a few things in your script if that works but let's see if it makes any difference first.
for all these tests make sure you actually upload a file ;)
then I do this
Basics of extracting data from CSV files [webmasterworld.com]
in what I laid out there you would replace step5. Instead of writing to file you would build your insert query and insert into mysql.