title: Update quantity after adding same product to shopping basket
body: <p>I can't figure out how to update the quantity of a product which is already in the shopping basket by adding it again. If the quantity is 1, after adding same product with  quantity 3, it should show 4 in the basket. All I can do at the moment is either replace the added quantity with the new one or insert an empty element in the basket table with no product details whatsoever.</p>
  
  <pre><code>&lt;?php
  // Products are added to the basket
  if (!isset($_SESSION['basket'])) {
    $_SESSION['basket'] = array();
  }
  
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['id'])) {
      $_SESSION['basket'][$_POST['id']] = array(
      'product_id'=&gt;($_POST['id']),
      'product_photo'=&gt;($_POST['hidden_photo']),
      'product_photo_alt'=&gt;($_POST['hidden_photo_alt']),
      'product_name'=&gt;($_POST['hidden_name']),
      'product_price'=&gt;($_POST['hidden_price']),
      'product_quantity'=&gt;($_POST['quantity'])
      );
    }
  }
  
  // This is the code with which I can only insert an empty element in the basket table with no product details.
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['id']) &amp;&amp; isset($_POST['quantity'])) {
      foreach ($_SESSION['basket'][$_POST['id']] as $item) {
          if ($_item['product_id'] === $_SESSION['basket']['product_id']) {
            $_SESSION['basket']['product_quantity'] += $item['product_quantity']; 
          } 
      }    
    }   
  }
  
  // If I apply below code it just replaces the quantity:
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['id']) &amp;&amp; isset($_POST['quantity'])) {
      foreach ($_SESSION['basket'] as $item) {
          if ($_item['product_id'] === [$_POST['id']]) {
            $item['product_quantity'] += $_POST['quantity']; 
          } 
      }    
    }   
  }
  ?&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/W9Obi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W9Obi.png" alt="Quantity replaced"></a>
  <a href="https://i.stack.imgur.com/36Wpf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/36Wpf.png" alt="Empty element inserted"></a></p>
  
  <p>Please let me know if you require more details. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I wrote another code but this time it just doubles the quantity sent to shopping basket. I also added one hidden input field on the page where products are listed.</p>
  
  <p>Code on the page where products are listed and where you add them to basket:</p>
  
  <pre><code>&lt;form&gt; // Submitted via Ajax
    &lt;div&gt;
      &lt;input type="hidden" name="id" value="&lt;?php echo $row['id']; ?&gt;"/&gt;
      &lt;input type="hidden" name="do_it" value="quantity_again"/&gt; // New input field added
      &lt;select name="quantity"&gt;
                  &lt;?php
                  // Drop-down select quantity menu 
                  for ($i=1; $i&lt;=$row['stock']; $i++) {
                  ?&gt;
        &lt;option value="&lt;?php echo $i; ?&gt;"&gt;&lt;?php echo $i; ?&gt;&lt;/option&gt;
  
                  &lt;?php
                  }   
                  ?&gt;
      &lt;/select&gt;
    &lt;/div&gt;
  &lt;input type="hidden" name="hidden_photo" value="&lt;?php echo $row['img_front_url']; ?&gt;"/&gt;
  &lt;input type="hidden" name="hidden_photo_alt" value="&lt;?php echo $row['img_front_alt'];?&gt;"/&gt;
  &lt;input type="hidden" name="hidden_name" value="&lt;?php echo $row['product']; ?&gt;"/&gt;
  &lt;input type="hidden" name="hidden_price" value="&lt;?php echo $row['price']; ?&gt;"/&gt;
          &lt;button class="add-to-basket"&gt;Add to basket&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The new code which only doubles the quantity in the basket. </p>
  
  <pre><code>if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['do_it']) &amp;&amp; $_POST['do_it'] == 'quantity_again') {
      foreach ($_SESSION['basket'] as &amp;$item) {
          if ($item['product_id'] === $_POST['id']) {
            $item['product_quantity'] += $_POST['quantity'];
          } 
      }    
    }   
  }
  </code></pre>
  
  <p>It seems that <code>$item['product_quantity']</code> and <code>$_POST['quantity']</code> are the same, so that's why the quantity gets doubled. 
  I thought that I would need to check first if <code>$_POST['quantity']</code> exists in the array <code>$_SESSION['basket']</code> and if it does, store it in a variable and then do <code>$item['product_quantity'] += $stored_qty</code> but that just again overrides the quantity. Here's the code:</p>
  
  <pre><code>if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['do_it']) &amp;&amp; $_POST['do_it'] == 'quantity_again') {
      foreach ($_SESSION['basket'] as $item) {
        if (in_array($_POST['quantity'], $_SESSION['basket'])) {
          $stored_qty = $_POST['quantity']; 
            if ($item['product_id'] === $_POST['id']) {
              $item['product_quantity'] += $stored_qty;
            } 
        }  
      }
    }   
  }
  
  </code></pre>