title: how to store cart session for logged in user using php
body: <p>Hello i am creating a simple web app, that users can log in and purchase food stuffs. i have used a tutorial i got online <a href="http://phppot.com/php/php-shopping-cart-with-jquery-ajax/" rel="nofollow">link to tutorial</a></p>
  
  <p>to implement the ordering aspect of this web app, 
  now i have implemented a user syatem so that users have to sign in to make an order.</p>
  
  <p>my problem is i noticed that the same cart information is shown to multiple users when logged in, which is wrong. every user should have their own different cart information based on what they might have added up.
  so i am asking how can i tie cart information to logged in users or each users.</p>
  
  <p>this is my code snippet</p>
  
  <p>product.php</p>
  
  <pre><code>&lt;head&gt;
      &lt;script&gt;
  function showEditBox(editobj,id) {
      $('#frmAdd').hide();
      $(editobj).prop('disabled','true');
      var currentMessage = $("#message_" + id + " .message-content").html();
      var editMarkUp = '&lt;textarea rows="5" cols="80" id="txtmessage_'+id+'"&gt;'+currentMessage+'&lt;/textarea&gt;&lt;button name="ok" onClick="callCrudAction(\'edit\','+id+')"&gt;Save&lt;/button&gt;&lt;button name="cancel" onClick="cancelEdit(\''+currentMessage+'\','+id+')"&gt;Cancel&lt;/button&gt;';
      $("#message_" + id + " .message-content").html(editMarkUp);
  }
  function cancelEdit(message,id) {
      $("#message_" + id + " .message-content").html(message);
      $('#frmAdd').show();
  }
  function cartAction(action,product_code) {
      var queryString = "";
      if(action != "") {
          switch(action) {
              case "add":
                  queryString = 'action='+action+'&amp;code='+ product_code+'&amp;quantity='+$("#qty_"+product_code).val();
              break;
              case "remove":
                  queryString = 'action='+action+'&amp;code='+ product_code;
              break;
              case "empty":
                  queryString = 'action='+action;
              break;
          }    
      }
      jQuery.ajax({
      url: "ajax_action.php",
      data:queryString,
      type: "POST",
      success:function(data){
          $("#cart-item").html(data);
          if(action != "") {
              switch(action) {
                  case "add":
                      $("#add_"+product_code).hide();
                      $("#added_"+product_code).show();
                  break;
                  case "remove":
                      $("#add_"+product_code).show();
                      $("#added_"+product_code).hide();
                  break;
                  case "empty":
                      $(".btnAddAction").show();
                      $(".btnAdded").hide();
                  break;
              }    
          }
      },
  
      error:function (){}
      });
  
  }
  
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="product-grid"&gt;
      &lt;?php
      $product_array = $db_handle-&gt;runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
      if (!empty($product_array)) { 
          foreach($product_array as $key=&gt;$value){
      ?&gt;
          &lt;div class="product-item"&gt;
              &lt;form id="frmCart"&gt;
  &lt;img width="100" src="&lt;?php echo $product_array[$key]["image"]; ?&gt;"&gt;
              &lt;div&gt;&lt;strong&gt;&lt;?php echo $product_array[$key]["name"]; ?&gt;&lt;/strong&gt;&lt;/div&gt;
              &lt;div class="product-price"&gt;&lt;?php echo "MUR".$product_array[$key]["price"]; ?&gt;&lt;/div&gt;
              &lt;div&gt;&lt;input type="range" name="quantity" id="qty_&lt;?php echo $product_array[$key]["code"]; ?&gt;"  value="1" min="1" max="10"&gt;
              &lt;?php
                  $in_session = "0";
                  if(!empty($_SESSION["cart_item"])) {
                      $session_code_array = array_keys($_SESSION["cart_item"]);
                      if(in_array($product_array[$key]["code"],$session_code_array)) {
                          $in_session = "1";
                      }
                  }
              ?&gt;
  
              &lt;input data-theme="a" type="button" data-icon="cutlery"  value="Add to cart" class="btnAddAction cart-action" onClick = "cartAction('add','&lt;?php echo $product_array[$key]["code"]; ?&gt;');cartAction('add','&lt;?php echo $product_array[$key]["code"]; ?&gt;')"  /&gt;
  
              &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;?php
              }
      }
      ?&gt;
  &lt;/div&gt;
  &lt;/div&gt;&lt;div class="clear-float"&gt;&lt;/div&gt;
  &lt;div id="shopping-cart"&gt;
  
  
  &lt;/div&gt;
  &lt;script&gt;
  $(document).ready(function () {
      cartAction('','');
  })
  &lt;/script&gt; 
  
  
  
  
  
  &lt;/div&gt;
  
  
  
  
  &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>and my ajax_action.php</p>
  
  <pre><code>&lt;?php
  session_start();
  require_once("dbcontroller.php");
  $db_handle = new DBController();
  
  if(!empty($_POST["action"])) {
  switch($_POST["action"]) {
      case "add":
          if(!empty($_POST["quantity"])) {
              $productByCode = $db_handle-&gt;runQuery("SELECT * FROM tblproduct WHERE code='" . $_POST["code"] . "'");
              $itemArray = array($productByCode[0]["code"]=&gt;array('name'=&gt;$productByCode[0]["name"], 'code'=&gt;$productByCode[0]["code"], 'quantity'=&gt;$_POST["quantity"], 'price'=&gt;$productByCode[0]["price"]));
  
              if(!empty($_SESSION["cart_item"])) {
                  if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
                      foreach($_SESSION["cart_item"] as $k =&gt; $v) {
                              if($productByCode[0]["code"] == $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 =&gt; $v) {
                      if($_POST["code"] == $k)
                          unset($_SESSION["cart_item"][$k]);
                      if(empty($_SESSION["cart_item"]))
                          unset($_SESSION["cart_item"]);
              }
          }
      break;
      case "empty":
          unset($_SESSION["cart_item"]);
      break;      
  }
  }
  ?&gt;
  &lt;?php
  if(isset($_SESSION["cart_item"])){
      $item_total = 0;
  ?&gt;  
  
  
  &lt;?php       
      foreach ($_SESSION["cart_item"] as $item){
          ?&gt;
                  &lt;img width="50" src="&lt;?php 
                  $img = $item["name"];
                  $sql = "select * FROM tblproduct where name = '$img'";
  $result = mysql_query($sql);
  while ($row = mysql_fetch_assoc($result)) {
      echo $row['image'];
  }
  ?&gt;" /&gt;&lt;input type="hidden" name="itemname[]" id="text-basic" value="&lt;?php echo $item["name"]; ?&gt;"&gt;&lt;input type="hidden" name="itemprice[]" id="text-basic" value="&lt;?php echo $item["price"]; ?&gt;"&gt;&lt;strong&gt;&lt;?php echo $item["name"]; ?&gt;&lt;/strong&gt;&lt;br /&gt;
  
  
                  &lt;?php echo "MUR".$item["price"]; ?&gt; x &lt;?php echo $item["quantity"]; ?&gt;
                  &lt;a style="color:#F30" href="#"  onClick="cartAction('remove','&lt;?php echo $item["code"]; ?&gt;')" class="btnRemoveAction cart-action"&gt;Remove Item&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
  
                  &lt;?php
          $item_total += ($item["price"]*$item["quantity"]);
          }
          ?&gt;
  
  &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Total:&lt;/strong&gt; &lt;?php echo "MUR".$item_total; ?&gt;&lt;input type="hidden" name="amount" id="text-basic" value="&lt;?php echo "N".$item_total; ?&gt;"&gt;      
    &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>i have tried to change the <code>$_SESSION["cart_item"]</code> to something that would always change <code>$_SESSION["MM_Username"]</code>, my thinking was that it would probably create the cart variable name based on who was logged in, but that approach didnt seem to go well, 
  <strong>can some one please help here</strong></p>