Hello, I have the follow xml file and I´m trying to upload into mysql table, but it don´t insert? any thing wong?
<?xml version=“1.0”?>
<resultset>
<line>
<nf>345</nf>
<nome>michael</nome>
</line>
</resultset>
Php codes:
<h1>Database update</h1>
<form enctype="multipart/form-data"
action="import.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
<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>
import.php
<?php
$data = array();
$con = mysql_connect("localhost","","");
if (!$con) {
die(mysql_error());
}
$db = mysql_select_db("db", $con);
if (!$db) {
die(mysql_error());
}
$sql = "select * from danfe";
$result = mysql_query($sql, $con);
if (!$result) {
die(mysql_error());
}
$total = mysql_num_rows($result);
if ($total > 0) {
$sql = "delete from danfe";
$result = mysql_query($sql, $con);
if (!$result) {
die(mysql_error());
}
}
function add_employee($nf, $nome)
{
global $data;
$con = mysql_connect("localhost","","");
if (!$con){ die(mysql_error());}
$db = mysql_select_db("db", $con);
if (!$db) {
die(mysql_error());
}
$nf = $nf;
$nome = $nome;
$sql = "INSERT INTO danfe (nf, nome) VALUES ('$nf', '$nome')";
mysql_query($sql, $con);
$data []= array('nf' => $nf, 'nome' => $nome);
}
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
global $last_row;
$last_row = false;
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$nf = "";
$nome = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $nf = $cell->nodeValue;
if ( $index == 2 ) $nome = $cell->nodeValue;
$index += 1;
}
if ($nf=='' and $nome=='') {
$last_row = true;
}
else {
add_employee($nf, $nome);
}
}
if ($last_row==true) {
$first_row = true;
}
else {
$first_row = false;
}
}
}
?>
<html>
<body>
<table>
<tr>
<th>List</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['nf'] ); ?></td>
<td><?php echo( $row['nome'] ); ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>