Jump to content

Recommended Posts

Hi...

 

I have codes in uploading .xml file to my database table, now I only need to save or insert only the data where ETD (Month from Date) is not previous Month.

 

ETD = date

 

Actually it's my first time to encounter this and while I'm posting this issue in forums., I also tried to find the answer.

here is my code:

 

<?php
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_now = date('m');

function add_employee($ETD,$PO_No,$SKUCode,$Description,$POReq ,$Comp)
  {
      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;
      $SKUCode = $SKUCode;
      $Description = $Description;
      $POReq = $POReq;
      $Comp = $Comp;
     

      $sql = "INSERT INTO sales_order (ETD,PO_No,SKUCode,Description,POReq,Comp) 
      VALUES 
      ('$ETD','$PO_No','$SKUCode','$Description','$POReq','$Comp')
      " or die(mysql_error());
      mysql_query($sql, $con);
      
       $data []= array('ETD'=>$ETD,'PO_No'=>$PO_No,'SKUCode'=>$SKUCode,'Description'=>$Description,'POReq'=>$POReq,'Comp'=>$Comp); 
}

if(empty($_FILES['file']['tmp_name'])){
$doc = new DOMDocument();
$dom = $doc->load('Sales1.xml');
      $rows = $doc->getElementsByTagName('Row');
      global $last_row;
      $last_row = false;
      $first_row = true;
      foreach ($rows as $row)
      {
          if ( !$first_row )
          {
              $ETD = "";
              $PO_No = "";
              $SKUCode = "";
              $Description = "";
              $POReq = "";
              $Comp = "";
              
              $index = 1;
              $cells = $row->getElementsByTagName( 'Cell' );
          
              foreach( $cells as $cell )
              { 
                  $ind = $cell->getAttribute( 'Index' );
                  if ( $ind != null ) $index = $ind;
              
                  if ( $index == 1 ) $ETD = $cell->nodeValue;  
                  if ( $index == 2 ) $PO_No = $cell->nodeValue;
                  if ( $index == 3 ) $SKUCode = $cell->nodeValue;
                  if ( $index == 4 ) $Description = $cell->nodeValue;
                  if ( $index == 5 ) $POReq = $cell->nodeValue;
                  if ( $index == 6 ) $Comp = $cell->nodeValue;
                  $index += 1;
              }

             if ($ETD=='' AND $PO_No=='' AND $SKUCode=='' AND $Description=='' AND $POReq=='' AND $Comp=='') {  
                    $last_row = true;
              }      
              else {
                    add_employee($ETD,$PO_No,$SKUCode,$Description, $POReq, $Comp);  
              }      
          }
          if ($last_row==true) {
              $first_row = true;
          }     
          else {
              $first_row = false;
          }
      }
  }  

  ?>

 

I tried this query:

 

$sql = "INSERT INTO sales_order (ETD,PO_No,SKUCode,Description,POReq,Comp) 
      VALUES 
      ('$ETD','$PO_No','$SKUCode','$Description','$POReq','$Comp')
      WHERE $ETD_month != '$date_now'" or die(mysql_error());

 

but still he get date where ETD(month from date) = March.

 

Thank you so much

Why aren't you just kicking it out on the PHP side of things?  Why should the query be preventing this insert?

 

The INSERT INTO statement when used with VALUES shouldn't accept a WHERE clause because it doesn't make sense at all.

 

~awjudd

I tried your suggested code.

 

Still all data was save :(

 

Thank you

 

$date_now = date('m');
      $ETD = strtotime($ETD);
$ETD_month = date("m", $ETD);

if($ETD_month!=$date_now){    
     
      $sql = "INSERT INTO sales_order (ETD,PO_No,SKUCode,Description,POReq,Comp) 
      VALUES 
      ('$ETD','$PO_No','$SKUCode','$Description','$POReq','$Comp')
     " or die(mysql_error());
  }  

Now this is my code to delete the previous month.

 

<?php
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());
}
    $sql = "delete from sales_order";
    $result =  mysql_query($sql);
    if (!$result) {
        die(mysql_error());
    }

  
function add_employee($ETD,$PO_No,$SKUCode,$Description,$POReq ,$Comp)
  {
      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;
      $SKUCode = $SKUCode;
      $Description = $Description;
      $POReq = $POReq;
      $Comp = $Comp;
      
    
     $sql = "INSERT INTO sales_order (ETD,PO_No,SKUCode,Description,POReq,Comp) 
      VALUES 
      ('$ETD','$PO_No','$SKUCode','$Description','$POReq','$Comp')
     ";
     $res_so = mysql_query($sql, $con);
     

      $sql = "INSERT INTO sales_order_dump (ETD,PO_No,SKUCode,Description,POReq,Comp) 
      VALUES 
      ('$ETD','$PO_No','$SKUCode','$Description','$POReq','$Comp')
      ON DUPLICATE KEY UPDATE
      ETD = '$ETD', PO_No = '$PO_No', SKUCode = '$SKUCode', Description = '$Description', POReq = '$POReq', Comp = '$Comp'" or die(mysql_error());
      $res = mysql_query($sql, $con);
      
       $data []= array('ETD'=>$ETD,'PO_No'=>$PO_No,'SKUCode'=>$SKUCode,'Description'=>$Description,'POReq'=>$POReq,'Comp'=>$Comp); 
}

  

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

$doc = new DOMDocument();
$dom = $doc->load('Sales1.xml');
        
      $rows = $doc->getElementsByTagName('Row');
      global $last_row;
      $last_row = false;
      $first_row = true;
      foreach ($rows as $row)
      {
          if ( !$first_row )
          {
              $ETD = "";
              $PO_No = "";
              $SKUCode = "";
              $Description = "";
              $POReq = "";
              $Comp = "";
              
              $index = 1;
              $cells = $row->getElementsByTagName( 'Cell' );
          
              foreach( $cells as $cell )
              { 
                  $ind = $cell->getAttribute( 'Index' );
                  if ( $ind != null ) $index = $ind;
                  if ( $index == 1 ) $ETD = $cell->nodeValue;  
                  if ( $index == 2 ) $PO_No = $cell->nodeValue;
                  if ( $index == 3 ) $SKUCode = $cell->nodeValue;
                  if ( $index == 4 ) $Description = $cell->nodeValue;
                  if ( $index == 5 ) $POReq = $cell->nodeValue;
                  if ( $index == 6 ) $Comp = $cell->nodeValue;

                                    $index += 1;
              }

             if ($ETD=='' AND $PO_No=='' AND $SKUCode=='' AND $Description=='' AND $POReq=='' AND $Comp=='') {  
                    $last_row = true;
              }      
              else {
                    add_employee($ETD,$PO_No,$SKUCode,$Description, $POReq, $Comp);  
              }      
          }
          if ($last_row==true) {
              $first_row = true;
          }     
          else {
              $first_row = false;
          }
      }
  }  

  $sql = "SELECT * FROM sales_order";
  $res = mysql_query($sql, $con);
  
  $row = mysql_fetch_row($res);
  
  if($row >= 0){
  $sql_del = "DELETE FROM sales_order WHERE MONTH(ETD) = MONTH(NOW())-1";
  $res_del = mysql_query($sql_del,$con) or die(mysql_error());
  }
   
   
    $sql = "UPDATE sales_order s SET 
   CompKg = (SELECT Bch_Wt FROM param_settings p WHERE s.Comp = p.Compounds ORDER BY p.Compounds),
   PlugWt = (SELECT Plug_Wt FROM param_settings p WHERE s.Comp = p.Compounds ORDER BY p.Compounds)";
   $res = mysql_query($sql, $con);

  $sql = "UPDATE sales_order SET 
  Doz = ((CompKg * 1000) / PlugWt) / 12 / 2,
  KgDoz = (CompKg / Doz),
  TotalKg = (POReq * KgDoz),
  BatchNeeded = (POReq / Doz)
  ORDER BY SKUCode";
  $res = mysql_query($sql, $con);
   
  ?>

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.