2017-02-08 1 views
0

상품이 이미 장바구니에 들어있는 경우 장바구니에 특정 상품을 추가 할 때 수량을 업데이트해야합니다. 현재 수량을 업데이트하는 대신 중복 된 항목이 추가됩니다. 아무도 제발 도와 줄 수 없어! 아래 코드.상품을 다시 추가 할 때 장바구니에 들어있는 상품의 수량이 증가합니다

case "add": 
    if(!empty($_POST["quantity"])) { 
     $productByCode = $db_handle->runQuery("SELECT * FROM medicine WHERE med_id='" . $_GET["med_id"] . "'"); 
     $itemArray = array(
      $productByCode[0]["med_id"]=>array(
       'name'=>$productByCode[0]["med_name"], 
       'med_id'=>$productByCode[0]["med_id"], 
       'image'=>$productByCode[0]["Image"], 
       'quantity'=>$_POST["quantity"], 
       'price'=>$productByCode[0]["unit_cost"] 
      ) 
     ); 

     if(!empty($_SESSION["cart_item"])) { 
      if(in_array($productByCode[0]["med_id"],$_SESSION["cart_item"])){ 
       foreach($_SESSION["cart_item"] as $k => $v) { 
        if($productByCode[0]["med_id"] == $k) 
         $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"]; 
       } 
      } 
      else { 
       $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
      } 
     } 
     else { 
      $_SESSION["cart_item"] = $itemArray; 
     } 
    } 
    break; 

case "remove": 
    if(!empty($_SESSION["cart_item"])) { 
     foreach($_SESSION["cart_item"] as $k => $v){ 
      if($_GET["med_id"] == $_SESSION["cart_item"][$k]['med_id']) 
       unset($_SESSION["cart_item"][$k]);   
      if(empty($_SESSION["cart_item"])) 
       unset($_SESSION["cart_item"]); 
     } 
    } 
    break; 
+0

당신이 당신의 카트를위한 플러그인이나 라이브러리를 사용하거나 직접 코딩 : 스위치에 추가의

function getMedsById($id,$db) { if(!is_numeric($id)) return false; $productByCode = $db->runQuery("SELECT * FROM medicine WHERE med_id='" . $id . "'"); $product = (!empty($productByCode[0]["med_id"]))? $productByCode[0] : false; if(empty($product)) return false; return array( 'name'=>$product["med_name"], 'med_id'=>$product["med_id"], 'image'=>$product["Image"], 'price'=>$product["unit_cost"] ); } function addToCart($array,$id,$qty) { $array['quantity'] = $qty; # Save item to cart $_SESSION["cart_item"][$id] = $array; } 

재 작업? – PhpDude

+0

$ _SESSION [ "cart_item"] [$ 수량]] = $ _POST [ "수량"];'$ _SESSION [ "cart_item"] [$ k] [ "수량 "] + = $ _POST ["quantity "];','+ =' – Rasclatt

+0

에 주목하십시오. 이렇게하지 마십시오 :''SELECT * FROM medicine where med_id = '". $ _GET [ "med_id"]. "" ". 당신은 SQL 주입 기회를 허용하고 있습니다. 'med_id'가 숫자라고 생각되면, 먼저 숫자인지 확인하고 그렇지 않으면 에러를 던집니다. 가장 쉬운 방법은 param을 바인딩하는 것입니다. – Rasclatt

답변

0

추가 문제를 해결하기 위해 함수를 사용하여 논리를 다시 작성하는 것이 도움이되는지 확인할 수 있습니다. 나는 또한 그것을 조금 청소할 것이다. 별도의 페이지 (들)에 기능을 저장하고 사용하기 전에 다음과 같습니다

case "add": 
    if(!empty($_POST["quantity"])) { 
     # Just do a basic ternary to avoid warnings 
     $id = (!empty($_GET["med_id"]))? $_GET["med_id"] : false; 
     # Check input not manipulated 
     $qty = (!empty($_POST['quantity']) && is_numeric($_POST['quantity']))? $_POST['quantity'] : 1; 
     # Use the function to get the item from db 
     $prod = getMedsById($id,$db_handle); 
     # Stop if return is empty (user may be trying to manipulate cart) 
     if(empty($prod)) 
      break; 
     # Same as yours 
     if(!empty($_SESSION["cart_item"])) { 
      # Check if this product is already set 
      if(isset($_SESSION["cart_item"][$id])){ 
       # If already set, just add the qty to existing 
       $_SESSION['cart_item'][$id]['quantity'] += $qty; 
       # Stop the add process here 
       break; 
      } 
     } 
     # Just add to cart here, it won't get here if already in cart 
     addToCart($prod,$id,$qty); 
    } 
    break; 
+0

$ _SESSION [ "cart_item"] [$ k] [ "quantity"] = $ _POST [ "quantity"];가 변경되었습니다. ~ $ _SESSION [ "cart_item"] [$ k] [ "수량"] + = $ _POST [ "수량"]; 그러나 여전히 같은 문제. 항목이 이미 장바구니에있는 경우 항목의 새 행을 추가합니다. 이전 수량의 항목을 변경해야합니다. 플러그인이나 라이브러리가 사용되지 않습니다. – Nikhil

+0

@Nikhil 내 기능을 사용해 보셨습니까? – Rasclatt

관련 문제