Forum Moderators: coopster

Message Too Old, No Replies

Data Upload Problem

Excel,PHP,MYSQL,Upload

         

antonioluis

6:46 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Hi,

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>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

jatar_k

6:51 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld antonioluis,

>> I keep getting an error on line 17 of my code

what error are you getting

antonioluis

7:08 pm on Oct 14, 2005 (gmt 0)

10+ Year Member




This is the error:
Notice: Undefined index: file in c:\program files\apache group\apache\htdocs\orbetech\orbeadmin\import2.php on line 17

jatar_k

7:15 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try dumping the $_FILES array and see what is actually in there that should help you get the right element

put this in the top of your script and submit the form, it will show you the array structure and where your naming could be wrong

echo '<pre>';
print_r($_FILES);
echo '</pre>';
die();

antonioluis

8:50 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



It just displys this:
Array
(
)

------------------
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>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

jatar_k

9:08 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks like it's empty then which would be part of the problem, I am assuming you selected a file to upload when you tried it.

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

antonioluis

12:43 am on Oct 15, 2005 (gmt 0)

10+ Year Member



Still not working.

Do you know a different method to insert data to a MYSQL database table using a xml spreadsheet?

antonioluis

1:00 am on Oct 15, 2005 (gmt 0)

10+ Year Member



I think is somethig related with "DOM Functions".

jatar_k

3:01 am on Oct 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I never parse excel sheets, I make people upload csv files which are easy enough to save as in excel

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.