Hi…
I have code for importing .xml file to database.
the problem is I cannot upload my file but no error display. And also I got a problem in importing data from .xml file to database but not all row will be save.
here is my code:
<?php
$data = array();
$con = mysql_connect("localhost", "root","");
if (!$con) {
die(mysql_error());
}
$db = mysql_select_db("mes", $con);
if (!$db) {
die(mysql_error());
}
$sql = "select * from sales_order";
$result = mysql_query($sql);
if (!$result) {
die(mysql_error());
}
function add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth)
{
global $data;
$con = mysql_connect("localhost", "root","");
if (!$con){ die(mysql_error());}
$db = mysql_select_db("mes", $con);
if (!$db) {
die(mysql_error());
}
$ProductType= $ProductType;
$WorkOrder = $WorkOrder;
$POIssueDate = $POIssueDate;
$SalesMonth = $SalesMonth;
$sql = "INSERT INTO sales_order (ProductType,WorkOrder,POIssueDate,SalesMonth)
VALUES
('$ProductType','$WorkOrder','$POIssueDate','$SalesMonth')" or die(mysql_error());
mysql_query($sql, $con);
$data []= array('ProductType'=>$ProductType,'WorkOrder'=>$WorkOrder,'POIssueDate'=>$POIssueDate,'SalesMOnth'=>$SalesMonth);
}
if ( $_FILES['file']['tmp_name'] ['error'])
//f (empty($_FILES['file']['tmp_name']['error']))
{ //$dom = DOMDocument::load('SalesOrder.xml');
$dom = DOMDocument::load($_FILES['file']['tmp_name']);
$dom = DOMDocument::__construct();
$rows = $dom->getElementsByTagName('Row');
global $last_row;
$last_row = false;
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$ProductType = "";
$WorkOrder = "";
$POIssueDate = "";
$SalesMonth = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $ProductType = $cell->nodeValue;
if ( $index == 2 ) $WorkOrder = $cell->nodeValue;
if ( $index == 3 ) $POIssueDate = $cell->nodeValue;
if ( $index == 4 ) $SalesMonth = $cell->nodeValue;
$index += 1;
}
if ($ProductType=='' AND $WorkOrder=='' AND $POIssueDate=='' AND $SalesMonth=='') {
$last_row = true;
}
else {
add_employee($ProductType,$WorkOrder,$POIssueDate,$SalesMonth);
}
}
if ($last_row==true) {
$first_row = true;
}
else {
$first_row = false;
}
}
}
?>
<html>
<body>
<table>
<tr>
<th>Sales Order</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['ProductType'] ); ?></td>
<td><?php echo( $row['WorkOrder'] ); ?></td>
<td><?php echo( $row['POIssueDate']) ;?> </td>
<td><?php echo( $row['SalesMonth'] ); ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
and I will attach my sample data and the data with color yellow background is only row I want to save to my database.
Thank you so much…