www.webdeveloper.com
Results 1 to 8 of 8

Thread: Insert only date where month is not previous month

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    Insert only date where month is not previous month

    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 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 == $ETD $cell->nodeValue;  
                      if ( 
    $index == $PO_No $cell->nodeValue;
                      if ( 
    $index == $SKUCode $cell->nodeValue;
                      if ( 
    $index == $Description $cell->nodeValue;
                      if ( 
    $index == $POReq $cell->nodeValue;
                      if ( 
    $index == $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:

    Code:
    $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

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    How about:

    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')

    mysql_query($sql, $con);
    }

    That way you only execute the query if the 2 months are different. You can refine the comparison condition depending on how the $EDT_month is generated.

  3. #3
    Join Date
    Oct 2011
    Posts
    350
    I tried your suggested code:

    and still all data from march - august was get.

    I tried to echo $date_now and $ETD_month the output are:

    $date_now = 04

    $ETD_month = 03 04 05 06 07 08.



    Thank you

  4. #4
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    Are you trying to saving data from months previous to the current month or just last month?

    The comparison should be the full date (year-month-day) instead of just the month. It gives you more accurate results.

    By the way where does the $ETD_month comes from.
    If $EDT is a full date then just compare that whole date with the current one using the same format (e.g Y-m-d)

  5. #5
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    Is $ETD_month = 03 04 05 06 07 08 the whole sequence of these numbers?
    If yes, then just have:
    $EDT_months=explode(' ', $EDT_month) to convert it into an array.

    Then
    if(!in_array($date_now,$EDT_months)){
    $sql="......"
    ....
    }

  6. #6
    Join Date
    Oct 2011
    Posts
    350
    Quote Originally Posted by holyhttp View Post
    Are you trying to saving data from months previous to the current month or just last month?

    The comparison should be the full date (year-month-day) instead of just the month. It gives you more accurate results.

    By the way where does the $ETD_month comes from.
    If $EDT is a full date then just compare that whole date with the current one using the same format (e.g Y-m-d)
    I'm trying to save data from the current month to the last month. I dont want to save data from the previous month which in my data is march.

    why should the comparison is (Y-m-d)?
    $EDT is a full date and $ETD_month is a variable where I only get the month from $ETD.

    Thank you

  7. #7
    Join Date
    Oct 2011
    Posts
    350
    Quote Originally Posted by holyhttp View Post
    Is $ETD_month = 03 04 05 06 07 08 the whole sequence of these numbers?
    If yes, then just have:
    $EDT_months=explode(' ', $EDT_month) to convert it into an array.

    Then
    if(!in_array($date_now,$EDT_months)){
    $sql="......"
    ....
    }
    I tried your suggested code:

    PHP Code:
    <?php
    //ini_set('display_errors', -1);
    //error_reporting(E_ALL);
    //error_reporting(-1);
    //error_reporting(E_ALL | E_STRICT);
    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 strtotime($ETD);
    $ETD_month date("m"$ETD);
          
    $PO_No $PO_No;
          
    $SKUCode $SKUCode;
          
    $Description $Description;
          
    $POReq $POReq;
          
    $Comp $Comp;

          
    $EDT_months=explode(' '$EDT_month);


    if(!
    in_array($date_now,$EDT_months)){
     
         
    $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 == 5 ) $ETD = $cell->nodeValue;  
                      if ( $index == 18 ) $PO_No = $cell->nodeValue;
                      if ( $index == 23 ) $SKUCode = $cell->nodeValue;
                      if ( $index == 24 ) $Description = $cell->nodeValue;
                      if ( $index == 48 ) $POReq = $cell->nodeValue;
                      if ( $index == 28 ) $Comp = $cell->nodeValue;
                      if ( $index == 29 ) $Cloth = $cell->nodeValue; */
                      
    if ( $index == $ETD $cell->nodeValue;  
                      if ( 
    $index == $PO_No $cell->nodeValue;
                      if ( 
    $index == $SKUCode $cell->nodeValue;
                      if ( 
    $index == $Description $cell->nodeValue;
                      if ( 
    $index == $POReq $cell->nodeValue;
                      if ( 
    $index == $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 "UPDATE sales_order s SET 
       CompKg = (SELECT Bch_Wt FROM param_settings p WHERE s.Comp = p.Compounds ORDER BY p.Compounds)"
    ;
       
    $res mysql_query($sql$con);

      
    ?>
    but it did not save.
    the output of $ETD_month actually is:

    3333333333333333333333334444444444444445555555555555556666666667777777777788888888888888

    Thank you

  8. #8
    Join Date
    Oct 2011
    Posts
    350
    When I echo the :

    $ETD_months ;

    output:

    ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray and so on.....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles