newphpcoder Posted April 26, 2012 Share Posted April 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/ Share on other sites More sharing options...
awjudd Posted April 26, 2012 Share Posted April 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340570 Share on other sites More sharing options...
newphpcoder Posted April 26, 2012 Author Share Posted April 26, 2012 So what should I do? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340576 Share on other sites More sharing options...
awjudd Posted April 26, 2012 Share Posted April 26, 2012 In you PHP code, before you run the INSERT statement, do a quick if statement to check $ETD_month != $date_now. if($ETD_month==$date_now) { // Insert } ~awjudd Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340581 Share on other sites More sharing options...
newphpcoder Posted April 26, 2012 Author Share Posted April 26, 2012 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340583 Share on other sites More sharing options...
awjudd Posted April 26, 2012 Share Posted April 26, 2012 I believe I had it flipped (because of your query). if($ETD_month==$date_now) { $sql= '...'; // Execute query. mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340587 Share on other sites More sharing options...
newphpcoder Posted April 26, 2012 Author Share Posted April 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340588 Share on other sites More sharing options...
newphpcoder Posted April 26, 2012 Author Share Posted April 26, 2012 I just want to get is from april to august. Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340589 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 For the record, please post the exact code you have right now. Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340986 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261611-insert-only-date-where-month-is-not-previous-month/#findComment-1340991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.