Jump to content

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


newphpcoder

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

18058_.doc

Link to comment
Share on other sites

Did you really insert a screen capture of Excel rendering an XML file into a Word file, and attach it with a 'sample data' label?

 

For one, you're using the global keyword. This makes debugging harder, no matter how simple the script.

Second, your condition for running the XML parsing code is that $_FILES['file']['tmp_name'] ['error'] evaluates to TRUE

Third, your add_employee() function is only used once. Does it really need to be a function?

 

Can you re-post your code, and comment the lines you're having issues with? I'm not entirely sure if you've copied and pasted this together, or if this is concious, misguided coding.

Link to comment
Share on other sites

<?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);
  }
//I having issue in this line first it did not import the data
[b] if ( $_FILES['file']['tmp_name'] ['error'])
//when I tried this commented code it import the data but It needs the .xml file is in the same folder of my php file.
//if (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;

//i think here is the code that an issue with saving not all columns only with yellow background.

      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;
          }
      }
  }[/b]  ?>
  
  <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>

Actually the .xml file is created in ms excel and I save it as .xml and I printscreen so that I post it

 

Thank you

 

 

Link to comment
Share on other sites

I got an error:

 

Notice: Undefined index: file in D:\Program Files\xampp\htdocs\MES PROJECT\import_salesorder.php on line 47

 

when I  used this code:

 

  error_reporting(E_ALL);   

 

line 47 is :

  if ( $_FILES['file']['tmp_name'] ['error']){ 

 

Thank you

 

I'm going to go ahead and guess it's because you the form tag in HTML lack a certain parameter...

enctype="multipart/form-data"

<form action="upload_file.php" method="post" enctype="multipart/form-data">

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.