Jump to content

problem in uploading .xml file where not all row will be import to the database


newphpcoder
 Share

Recommended Posts

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..

post-42557-0-66000400-1334280659_thumb.jpg

Link to comment
Share on other sites

if ( $_FILES['file']['tmp_name'] ['error'])
it will return 0 if file upload succed. 0 will be converted to boolean false. from your condition the if part will never be executed if file uplaod succeded.
if ( !$_FILES['file']['tmp_name'] ['error'])

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...