2017-11-30 8 views
0

に私のコードを改訂しました機能商品が既にカートに入っていれば、数量を更新しないといくつかの問題があります。 。 カートに追加された最初の製品のみが正しく更新されます。は、カートのコードに追加UPDATEDショッピングカート - 数量カートの最初の項目のみを更新します。

Example of add to cart not updating quantity

は次のとおりです。私は、コードで何かを

// -------------------------  
// Adding item 
// -------------------------  
case "add": 
    if(!empty($_POST["quantity"])) { 
     $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'"); 
     $itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"], 
     'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"])); 

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

足りませんか?

ありがとうございました。 スタン。

編集: 更新されたコードのdoesntの仕事:

// -------------------------  
// Adding item 
// -------------------------  
case "add": 
    if(!empty($_POST["quantity"])) { 
     $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'"); 
     $itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"], 
     'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"])); 

     if(!empty($_SESSION["cart_item"])) { 
      if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) { 
       foreach($_SESSION["cart_item"] as $k => $v) { 
         if($productByCode[0]["product_id"] == $k) { 
          if(empty($_SESSION["cart_item"][$k]["quantity"])) { 

           **foreach($_SESSION["cart_item"] as $k =>&$v){ 

            $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; 

           }** 

          } 
          $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; 
         } 
       } 
      } else { 
       $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
      } 
     } else { 
      $_SESSION["cart_item"] = $itemArray; 
     } 
    } 
break; 
+1

あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃の脆弱性があります。あなたは[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)ドライバ。 [**この記事**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)には、いくつかの良い例があります。 –

+0

ライブURLとデータベース構造に関する情報を投稿したので、すぐにオフラインにすることをお勧めします*。ネット上の誰でもあなたのデータベースを抹消することができます。 –

+0

ここでデモにリンクするのは最良の方法ではありません。 [SOの最小限の検証可能なサンプルを作成する方法](https://stackoverflow.com/help/mcve)をご覧ください。フォームのhtmlを含めてください。 –

答えて

0

炎のスーツに:

あなたはこの

$_SESSION["cart_item"][$v]["quantity"] = $v++; 

PHPは、実際には配列のコピーを作成しなかったとき、私は信じていますあなたは繰り返しています

この配列に書き込みをしたいので、あなたが明示的に&

foreach($_SESSION["cart_item"] as $k =>&$v)... 
+0

私の謝罪は、出力をテストするために私の行が変更されました。私は今元のポストを動作するコードで更新しました。 –

+0

基本的な前提は残っています。一度配列に書き込もうとすると、値ではなく参照として行う必要があります。 – Kisaragi

+0

上記で使用されたコードを表示するためにメインポストを編集しました。私はそれがなぜ最初の製品のために働くのか理解していないが、その後は全く理解できない。 –

0

これは、このここ はイムセッションにデータを格納 $ pr_id =製品Idを

adding item to cart 
    if(isset($_SESSION['cart_items'])) 
     { 
      //find index of an array if it exits then do not add else add to cart and then 
      if (!array_key_exists($pr_id,$_SESSION['cart_items'])) 
      { 
       $_SESSION['cart_items'][$pr_id]['id'] = $pr_id; 
       $_SESSION['cart_items'][$pr_id]['quantity'] = $quantity; 
      } 
     } 
     else 
     { 
      $_SESSION['cart_items'][$pr_id]['id'] = $pr_id; 
      $_SESSION['cart_items'][$pr_id]['quantity'] = $quantity;   
     } 
     $arr = array(); 
     $arr['count'] = count($_SESSION['cart_items']); 
     $arr['message'] = 'Item successFully added'; 
     echo json_encode($arr); 

を試してみてくださいを使用して参照することによって反復処理すべきですあなたは

製品のエントリまたは数量を更新するためのDB

に更新エントリを作成することができます動作するコード

+0

ありがとう、私は本当にすべてのコードを変更したくない。本当にシンプルなものが欠けているはずです。 –

関連する問題