  | 
              
	              
	                
  
    
	 
        PHP การ Update ข้อมูลที่มีอยู่แล้วหากไม่มีให้ Insert ข้อมูลใหม่เข้าไปแทน     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                Code (PHP) 
<meta charset=utf-8 >
<?php
session_start();
require("../../function/function.php");
connect_db();
$tableid=$_SESSION["table_tid"];
$empname=$_SESSION["empname"];
$staid=$_SESSION["tbsta"];
$_SESSION["cmt"]=$_POST["comment"];
$comment=$_SESSION["cmt"];
// SELECT ORDER ล่าสุดเพื่อทำการเพิ่มรายการอาหารนั้นน
 $order=mysql_query("SELECT order_id, table_id FROM order_id WHERE table_id='$_SESSION[tid]' AND order_status='0'") or die(mysql_error());
 list($order_id, $tl_id)=mysql_fetch_row($order);
   foreach ($_SESSION["cart_item"] as $item)
   {
     // SELECT ชื่อออกมาเพื่อแสดงเป็นภาษาไทย
     $nam=$item["id"];
      $namethai=mysql_query("SELECT menu_name, menu_code FROM list_menu WHERE menu_id='$nam'");
     list($nathai,$randn)=mysql_fetch_row($namethai);
       $id=$item["id"];
       $name=$item["name"];
       $quantity=$item["quantity"];
       $price=$item["price"];
       $total_price = ($item["price"]*$item["quantity"]);
       $mcode=$item["code"];
     }
      unset($_SESSION["cart_item"]);
      $chk = mysql_query("SELECT order_detail_id, order_id, menu_code, order_amount, total_order_price FROM order_detail WHERE order_id='$order_id'") or die (mysql_error());
       while(list($odtid, $oid, $mnu, $oa, $toa)=mysql_fetch_row($chk)){
      $update = "UPDATE order_detail
        SET  order_amount=$oa + $quantity,
             total_order_price=$toa + $total_price
        WHERE order_id='$order_id' AND menu_code='$randn'";
       mysql_query($update) or die (mysql_error());
     }
     if($mnu!=$rand){
     //INSERT รายการอาหารลงในตาราง ORDER DETAIL
       $insert="INSERT INTO order_detail VALUES('','$order_id','$randn','$nathai','$quantity','$comment','$price','$total_price','$empname','')";
       mysql_query($insert) or die (mysql_error());
     }
?>
<script> alert('successfully')</script>
<script>window.location='proprietor.php?page=plodr&get_id=<?php echo $tl_id;?>'</script>;
]
 
 
 
 
ผมต้องการ update ออเดอร์อาหารที่มีอยู่แล้วให้เพิ่มจำนวนเข้าไป แต่ถ้าเมนูไหนยังไม่มีให้ insert เมนูใหม่เพิ่มเข้าไปต้องตั้งเงื่อนไขยังไงครับ
 
 
  Tag : PHP               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2018-07-22 08:29:15 2018-07-22 08:30:53	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-07-22 08:27:40 | 
                      By :
                          pcthm06 | 
                      View :
                          1750 | 
                      Reply :
                          5 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 select where ก่อน 
 
ถ้า num > 0 
ให้ update 
ถ้า num =0  
ให้ insert                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-07-22 09:32:17 | 
                        By :
                            Pong Thep | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
$con->query( $update_sql );
if( $con->affected_rows()==0){
   $con->query( $insert_sql);
}
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-07-22 10:12:58 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ยังไม่ค่อยเข้าใจอะครับต้องนำไปใช้อย่างไร ยังเป็นมือใหม่อยู่รบกวนด้วยคับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-07-23 15:08:02 | 
                        By :
                            pcthm06 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตอนนี้ได้แล้วนะคับ  
Code (PHP) 
$chk = mysql_query("SELECT order_detail_id, order_id, menu_code, order_amount, total_order_price FROM order_detail WHERE order_id='$order_id'") or die (mysql_error());
        while(list($odtid, $oid, $mnu, $oa, $toa)=mysql_fetch_row($chk)){
       $update = "UPDATE order_detail
         SET  order_amount=$oa + $quantity,
              total_order_price=$toa + $total_price
         WHERE order_id='$order_id' AND menu_code='$randn'";
        mysql_query($update) or die (mysql_error());
      }
      $affected_rows = mysql_affected_rows();
      if($affected_rows==0){
      //INSERT รายการอาหารลงในตาราง ORDER DETAIL
        $insert="INSERT INTO order_detail VALUES('','$order_id','$randn','$nathai','$quantity','$comment','$price','$total_price','$empname','')";
        mysql_query($insert) or die (mysql_error());
      }
 
 
แต่ตอนนี้ติดตรง update คือ ถ้าเราทำการสั่งอาหารเพิ่มจากตอนแรก  
เมนู 1 มีจำนวน 2 ชิ้น  
เมนู 2 มี 1 ชิ้น  
 
แล้วจะสั่งเพิ่มไปอย่างละ 1 ชิ้น 
มันควรจะอัพเดทเป็น เมนู 1 มีจำนวน 3 ชิ้น  
                               เมนู 2 มี 2 ชิ้น 
 
แต่ผลที่ออกมาเป็น เมนู 1 มีจำนวน  2 ชิ้น  
                               เมนู 2 มี 2 ชิ้น 
คือมันจะเอา จำนวนสุดท้ายที่กดไปอัพเดทจำนวนของรายการอื่นน่ะครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-07-23 16:07:41 | 
                        By :
                            pcthm06 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ผมเก็บเป็นรูปแบบของ array ครับ 
 
Code (PHP) 
for ($i = 0; $i < PHP_INT_MAX; $i++) {
  // ตรวจว่ามีข้อมูลตามค่า $i หรือไม่
  if (!isset($_POST['aaa'][$i],$_POST['bbb'][$i])){
    // ถ้าไม่มี แสดงว่าไม่มีข้อมูลส่งมา หรือข้อมูลครบแล้ว ก็ออกจากลูปซะ
    break;
  }
  $orderno .= "@".$_POST['aaa'][$i];
  $num .= "@".$_POST['bbb'][$i];
}
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-07-30 15:54:57              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-07-30 15:15:28 | 
                        By :
                            2037881702931155 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |