title: Adding product quantity to cart in php only if product is available in cart with same color and size
body: <p>I've created php ekart site just for testing purpose. My query is when I click on add to cart it should firstly validate whether any one color and any one size is selected, if both have been selected it adds product to the cart this works fine but if I add same product but with different color or size it overlaps the same product which was already added and that products quantity turns to 1 but if I select another product it works well but same thing goes when I select different size or color. Since I am a beginner at php, I'm not sure where am I going wrong here.</p>
  
  <p>Php code of Product add to cart is given Below:</p>
  
  <pre><code>$sql = "SELECT * FROM allprods WHERE listing = '$listing' and  id = '$id'";
  $data = mysql_query($sql);
  if (mysql_num_rows($data) == 1)
  {
      $row = mysql_fetch_array($data);
      if (isset($_SESSION['cart'][$id]))
      {
          if(isset($_SESSION['cart'][$id][$color]) &amp;&amp; isset($_SESSION['cart'][$id][$size]))
          {
              $_SESSION['cart'][$id]['quantity']++;
              echo "&lt;script&gt;alert('".$row['product_name']." has been added to your cart.');&lt;/script&gt;";
          }
          else
          {
              $_SESSION['cart'][$id] = array('quantity' = &gt;1, 'price' =&gt; $row['product_special_price'], 'cat' =&gt; $cat, 'id' =&gt; $id, 'size' =&gt; $size, 'color' =&gt; $color, 'name' =&gt; $row['product_name']);
              echo "&lt;script&gt;alert('" . $row['product_name'] . " has been added to your cart.');&lt;/script&gt;";
          }
      }
      else
      {
          $_SESSION['cart'][$id] = array('quantity' =&gt; 1, 'price' =&gt; $row['product_special_price'], 'cat' =&gt; $cat, 'id' =&gt; $id, 'size' =&gt; $size, 'color' =&gt; $color, 'name' =&gt; $row['product_name']);
  
          echo "&lt;script&gt;alert('" . $row['product_name'] . " has been added to your cart.');&lt;/script&gt;";
      }
  }
  </code></pre>
  
  <p>Cart program code</p>
  
  <pre><code>&lt;?php
      if(isset($_POST['qty']))
      {
          foreach($_POST['qty'] as $product_id=&gt;$item_qty)
          {
              $id=(int)$product_id;
              $qty=(int)$item_qty;
              if($qty==0)
              {
                  unset($_SESSION['cart'][$id]);
              }
              elseif($qty&gt;0)
              {
                  $_SESSION['cart'][$id]['quantity']=$qty;
              }
          }
      }
      else
      {
          echo "";
      }
      if(!empty($_SESSION['cart']))
      {
          require "../link_db.php";
          $sql="SELECT * FROM allprods WHERE id IN(";
          foreach($_SESSION['cart'] as $id=&gt;$value)
          {
              $sql.=$id.',';
          }
          $sql=substr($sql,0,-1).') ORDER BY product_id ASC'; 
          $data=mysql_query($sql);
          while($row=mysql_fetch_array($data))
          {
  ?&gt;
  Table row and data goes here!!
  &lt;?php
          }
          else
          {
  ?&gt;
  &lt;tr&gt;
      &lt;th colspan='4' class='noprodavailablewrap'&gt;
          There are no products in your cart.
      &lt;/th&gt;
  &lt;/tr&gt;
  &lt;?php
      }
  ?&gt;
  </code></pre>
  
  <p>DB structure given below:
  <a href="https://i.stack.imgur.com/A7jm8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A7jm8.png" alt="Image of database structure"></a>
  <a href="https://i.stack.imgur.com/qITxq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qITxq.png" alt="Image of database structure"></a></p>