title: Add items to cart in php using sessions
body: <p><a href="https://i.stack.imgur.com/EeQpw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EeQpw.png" alt="How cat looks like, there are items added twice"></a>Am trying to update the quantity in add to cart session, but the below code only increases the quantity of the first item added twice and then perfoms a merge whenever any other item is added twice, kidly help</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$('.add-to-cart-mt').on('click', function(e){
          e.preventDefault();
          var id =$(this).attr('id');
          $('#add-to-cat-dialog').dialog({
              autoOpen:false,
              modal:true,
              hide:"pluff",
              show:"slide",
              height:200,
              open: function() { $(".ui-dialog-titlebar-close").hide(); },
              
              buttons:{
                  "Add":function (){
                      $('#add-to-cat-dialog').dialog("close");
                      $.ajax({
                          url: 'add_to_cart.php',
                          data: {  productId : id },
                          
                          success: function (data) {
                              $('.top-cart-contain').empty();
                              $('.top-cart-contain').load("header_cart_summary.php");
                          },
                          
                          error :function (data, textStatus, jqXHR) {  }
                      });
                  },
                  
                  "Cancel":function (){
                      $(this).dialog("close");
                  }
              }
          });
          
          $('#add-to-cat-dialog').dialog("open");
      });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>session_start();
  require './database.php';
  
        if(!empty($_GET["productId"])) {
            $id=$_GET["productId"];
              $productById = $connection-&gt;query("SELECT * FROM products WHERE productId='$id';");
              if($productById &amp;&amp; (mysqli_num_rows($productById)&gt;0)){
                  echo 'query ok';
                  $productDetail= mysqli_fetch_assoc($productById);
                  $itemArray = array(
                      $productDetail["productId"]=&gt;array(
                          'name'=&gt;$productDetail["productName"],
                          'id'=&gt;$productDetail["productId"], 
                          'quantity'=&gt;1,
                          'price'=&gt;$productDetail["productPrice"],
                          'image'=&gt;$productDetail["productsImage1"]
                          )
                      );
              
                  if(!empty($_SESSION["cart_item"])) {
                      if(in_array($productDetail["productId"],array_keys($_SESSION["cart_item"]))) {
                          foreach($_SESSION["cart_item"] as $k =&gt; $v) {
                              if($productDetail["productId"] == $k) {
                                    if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                          $_SESSION["cart_item"][$k]["quantity"] = 0;
                                    }
                                    $_SESSION["cart_item"][$k]["quantity"] += 1;
                              }
                          }
                      } else {
                            $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                      }
                  } else {
                        $_SESSION["cart_item"] = $itemArray;
                  }
                  
                  //set product count in the bag
                  if(!empty($_SESSION['cart_volume'])){
                      $_SESSION['cart_volume'] += 1;
                  } else {
                      $_SESSION['cart_volume'] = 1;
                  }
                  
              }else {echo ''. mysqli_error($connection);}
              
        } else {
            echo 'no item';
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>[enter image description here][1]&lt;li class="item col-lg-4 col-md-4 col-sm-6 col-xs-6 "&gt;
                                  &lt;div class="product-item"&gt;
                                    &lt;div class="item-inner"&gt;
                                      &lt;div class="product-thumb has-hover-img"&gt;
                                        &lt;figure&gt; &lt;a title="Ipsums Dolors Untra" href="single_product.html"&gt; &lt;img class="first-img" src="./productImages/&lt;?php echo $item['productsImage1']; ?&gt;" style="height: 250px;"  alt=""&gt; &lt;img class="hover-img" src="../images/products/img05.jpg" alt=""&gt; &lt;/a&gt;&lt;/figure&gt;
                                        &lt;div class="pr-info-area animated animate2"&gt;&lt;a href="quick_view.html" class="quick-view"&gt;&lt;i class="fa fa-search"&gt;&lt;span&gt;Quick view&lt;/span&gt;&lt;/i&gt;&lt;/a&gt; &lt;a href="wishlist.html" class="wishlist"&gt;&lt;i class="fa fa-heart"&gt;&lt;span&gt;Wishlist&lt;/span&gt;&lt;/i&gt;&lt;/a&gt; &lt;a href="compare.html" class="compare"&gt;&lt;i class="fa fa-exchange"&gt;&lt;span&gt;Compare&lt;/span&gt;&lt;/i&gt;&lt;/a&gt; &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class="item-info"&gt;
                                        &lt;div class="info-inner"&gt;
                                          &lt;div class="item-title"&gt; &lt;h4&gt;&lt;a title="Ipsums Dolors Untra" href="single_product.html"&gt;&lt;?php echo $item['productName']; ?&gt;&lt;/a&gt;&lt;/h4&gt; &lt;/div&gt;
                                          &lt;div class="item-content"&gt;
                                            &lt;div class="rating"&gt; &lt;i class="fa fa-star-o"&gt;&lt;/i&gt; &lt;i class="fa fa-star-o"&gt;&lt;/i&gt; &lt;i class="fa fa-star-o"&gt;&lt;/i&gt; &lt;i class="fa fa-star-o"&gt;&lt;/i&gt; &lt;i class="fa fa-star-o"&gt;&lt;/i&gt; &lt;/div&gt;
                                            &lt;div class="item-price"&gt;
                                              &lt;div class="price-box"&gt;
                                                &lt;p class="special-price"&gt; &lt;span class="price-label"&gt;Special Price&lt;/span&gt; &lt;span class="price"&gt;Ksh. &lt;?php echo number_format($item['productPrice'], 2) ; ?&gt;&lt;/span&gt; &lt;/p&gt;
                                                &lt;!--&lt;p class="old-price"&gt; &lt;span class="price-label"&gt;Regular Price:&lt;/span&gt; &lt;span class="price"&gt; $567.00 &lt;/span&gt; &lt;/p&gt;--&gt;
                                              &lt;/div&gt;
                                            &lt;/div&gt;
                                            &lt;div class="pro-action"&gt;
                                                &lt;button type="button" class="add-to-cart-mt" id="&lt;?php echo $item['productId']; ?&gt;"&gt; &lt;i class="fa fa-shopping-cart"&gt;&lt;/i&gt;&lt;span&gt; Add to Cart&lt;/span&gt; &lt;/button&gt;
                                            &lt;/div&gt;
                                          &lt;/div&gt;
                                        &lt;/div&gt;
                                      &lt;/div&gt;
                                    &lt;/div&gt;
                                  &lt;/div&gt;
                                &lt;/li&gt;</code></pre>
  </div>
  </div>
  </p>