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

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…

Hi newphpcoder!

Have you tried setting PHP errors on?
You could set it in the begining of your file like:

ini_set(‘display_errors’, 1);
error_reporting(E_ALL);

HTH.

Yes, I tried but still no output.

Now my problem is what if my .xml file is not located in my web server?

Thank you

Now, I tried to upload my actual data:

using this code:


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
   // error_reporting(0);
  date_default_timezone_set("Asia/Singapore"); //set the time zone
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';
    die();
}

mysql_select_db("mes", $con);

$date_upload = date('Y-m-d');
?>
<html>
<head>
<title>Sales Order</title>
<link rel="stylesheet" type="text/css" href="kanban.css" />
<style type="text/css">
  #upload{
    position: relative;
    font-family: Arial, Helvetica, sans-serif;
    font-size: .9em;
    margin-left: 10px;
    margin-right: 50em;
    width: 500px;
    height: auto;
    float: left;
    top : 10px;
}
#kanban_table {
    position: relative;
    width: auto;
    height: auto;
    top : 15px;
    float:left;
    margin-left: 10px;
}
table {
    margin: 7px;
    width:auto;
    font-family: Arial, Helvetica, sans-serif;
    font-size: .9em;
    border: 1px solid #DDD;
}

th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: .7em;
    background: #694;
    color: #FFF;
    padding: 2px 6px;
    border-collapse: separate;
    border: 1px solid #000;
}

td {
    font-family: Arial, Helvetica, sans-serif;
    font-size: .7em;
    border: 1px solid #DDD;
    text-align: left;
}
</style>
<script type="text/javascript">
function upload_so(){
    var date_upload = document.getElementById('date_upload').value;
    document.salesorder.action="import_salesorder.php?date_upload="+date_upload;
    document.salesorder.submit();

    //alert("Sales Order Successfully Uploaded.");
    //window.location="SalesOrder.php";
}
</script>
</head>
<body>
<!--<form enctype="multipart/form-data" action="import_attendance.php" method="post" name="salesorder"> -->
<form enctype="multipart/form-data" action="" method="post" name="salesorder">
<div id="ddcolortabs">
<ul>
<li id="current"> <a href="SalesOrder.php" title="Sales Order"><span>Order Management</span></a></li>
<li> <a href="ParameterSettings.php" title="Parameter Settings"><span>Parameter Settings</span></a></li>
<li style="margin-left: 1px"><a href="kanban_report.php" title="WIP Report"><span>WIP Report</span></a></li>
<li><a href="fsn.php" title="Finished Stock Note"><span>WMS FG</span></a></li>
<li><a href="ReceivingMaterials.php" title="WMS RM"><span>WMS RM</span></a></li>
</ul>
</div>
<div id="ddcolortabs1">
<ul>
<li id="current"><a href="SalesOrder.php" title="Sales Order"><span>Sales Order</span></a></li>
<li><a href="JobOrder.php" title="Job Order"><span>Job Order</span></a></li>
</ul>
</div>
<div id="upload">
<fieldset>
<legend>Upload Sales Order</legend>
 <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
  <table width="500">
  <tr>
  <td>Sales Order:</td>
  <td><input type="file" name="file" /></td>
  <td><input type="button" value="Upload" onclick="upload_so()"/></td>
  </tr>
  </table>
</fieldset>
</div>
<input type="hidden" value="<?php echo $date_upload; ?>" id="date_upload" name="date_upload">

</div>
<div id="kanban_table">
<?php
echo "<table border='.5'>";
echo "<th>ETD</th>
<th>PO No.</th>
<th>Code</th>
<th>Cases</th>
<th>UoM</th>
<th>Description</th>
<th>Remarks</th>
";
$sql = "SELECT ETD, PO_No, Code, Cases, UoM, Description, Remarks FROM sales_order ORDER BY ETD";
$res = mysql_query($sql, $con);

while($row = mysql_fetch_assoc($res)){
    $ETD = $row['ETD'];
    $PO_No = $row['PO_No'];
    $Code = $row['Code'];
    $Cases = $row['Cases'];
    $UoM = $row['Uom'];
    $Description = $row['Description'];
    $Remarks = $row['Remarks'];

echo "<tr>
<td>$ETD</td>
<td>$PO_No</td>
<td>$Code</td>
<td>$Cases</td>
<td>$UoM</td>
<td>$Description</td>
<td>$Remarks</td>
</tr>";
}
?>
</div>
</form>
</body>
</html>

but it did not save in database :frowning:

What does import_salesorder.php do?
You are only passing the date to that script.

No…

in import_saleorder.php where the code for importing data from .xml to database and also to save the date wehre the data upload.

Thank you

What is actually your problem?
You say that your data isn’t getting saved to the database but you have not shown any code that does an INSERT or UPDATE. How can we help if we can’t see the problem?
You need to turn on some kind of error reporting or try to log the erros in a file.

Now It save to the database, by counting the rows which needs to save to the database, now in my importing.xml file it did not import .xml file where did not in the same folder where the php file location.

for example my .xml file is in desktop.

It did not save it.

here is my import.php


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

//error_reporting(E_ALL ^ E_NOTICE); 
  date_default_timezone_set("Asia/Singapore"); //set the time zone    
$data = array();

$con = mysql_connect("localhost", "root","");
if (!$con) { 
  die(mysql_error());
}
$db = mysql_select_db("mes", $con);
if (!$db) { 
  die(mysql_error());
}

$date_upload = $_POST['date_upload'];
//====check if date_upload is greater than 1 month=====//
 if(strtotime($date_upload) > strtotime('1 month ago')){

    $sql = "delete from so_dateupload";
    $result =  mysql_query($sql);
    if (!$result) {
        die(mysql_error());
    }
  
  $sql = "INSERT INTO so_dateupload (date_upload) VALUES ('$date_upload')";
  $res_date = mysql_query($sql, $con); 
 }
 else {
     $sql = "INSERT INTO so_dateupload (date_upload) VALUES ('$date_upload')";
  $res_date = mysql_query($sql, $con);   
 }
 
//=====deleter sales_order if greater than one month=====//
     if(strtotime($date_upload) > strtotime('1 month ago')){

    $sql = "delete from sales_order";
    $result =  mysql_query($sql);
    if (!$result) {
        die(mysql_error());
    }
 }

 
 
 
  
function add_employee($ETD,$PO_No,$Code,$Cases,$UoM,$Description,$Remarks)
  {
      global $data;
      
      
      $con = mysql_connect("localhost", "root","");
      if (!$con){ die(mysql_error());}
      $db = mysql_select_db("mes", $con);
      if (!$db) { 
          die(mysql_error());
      }

      $ETD= $ETD;
      $PO_No = $PO_No;
      $Code = $Code;
      $Cases = $Cases;
      $UoM = $UoM;
      $Description = $Description;
      $Remarks = $Remarks;
      

      $sql = "INSERT INTO sales_order (ETD,PO_No,Code,Cases,UoM,Description,Remarks) 
      VALUES 
      ('$ETD','$PO_No','$Code','$Cases','$UoM','$Description','$Remarks')
      ON DUPLICATE KEY UPDATE
      ETD = '$ETD', PO_No = '$PO_No', Code = '$Code', Cases = '$Cases', UoM = '$UoM', Description = '$Description', Remarks = '$Remarks'" or die(mysql_error());
      mysql_query($sql, $con);
      
       $data []= array('ETD'=>$ETD,'PO_No'=>$PO_No,'Code'=>$Code,'Cases'=>$Cases,'UoM'=>$UoM,'Description'=>$Description,'Remarks'=>$Remarks); 
}
 // if ( $_FILES['file']['tmp_name']){
 if (empty($_FILES['file']['tmp_name']['error']))  
  { $dom = DOMDocument::load('SalesOrder.xml');   
  //  $dom = DOMDocument::load($_FILES['file']['tmp_name'] ['error']);
    // $dom = DOMDocument::__construct();           
      $rows = $dom->getElementsByTagName('Row');
      global $last_row;
      $last_row = false;
      $first_row = true;
      foreach ($rows as $row)
      {
          if ( !$first_row )
          {
              $ETD = "";
              $PO_No = "";
              $Code = "";
              $Cases = "";
              $UoM = "";
              $Description = "";
              $Remarks = "";
              
              $index = 1;
              $cells = $row->getElementsByTagName( 'Cell' );
          
              foreach( $cells as $cell )
              { 
                  $ind = $cell->getAttribute( 'Index' );
                  if ( $ind != null ) $index = $ind;
              
                  if ( $index == 5 ) $ETD = $cell->nodeValue;  
                  if ( $index == 20 ) $PO_No = $cell->nodeValue;
                  if ( $index == 25 ) $Code = $cell->nodeValue;
                  if ( $index == 51 ) $Cases = $cell->nodeValue;
                  if ( $index == 59 ) $UoM = $cell->nodeValue;
                  if ( $index == 26 ) $Description = $cell->nodeValue;
                  if ( $index == 69 ) $Remarks = $cell->nodeValue;
                  $index += 1;
              }

             if ($ETD=='' AND $PO_No=='' AND $Code=='' AND $Cases=='' AND $UoM=='' AND $Description=='' AND $Remarks=='') {  
                    $last_row = true;
              }      
              else {
                    add_employee($ETD,$PO_No,$Code,$Cases, $UoM, $Description, $Remarks);  
              }      
          }
          if ($last_row==true) {
              $first_row = true;
          }     
          else {
              $first_row = false;
          }
      }
  }
 
  ?>

  



Thank you