Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2012, 02:34 AM   PM User | #1
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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:

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:

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
newphpcoder is offline   Reply With Quote
Old 04-26-2012, 03:31 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
you can't have a WHERE clause in an INSERT.
guelphdad is offline   Reply With Quote
Old 04-26-2012, 03:36 AM   PM User | #3
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Quote:
Originally Posted by guelphdad View Post
you can't have a WHERE clause in an INSERT.
So what would be the possible thing should I do?

Thank you
newphpcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.