title: PHP: How to store cart data (foreach loop) into different variables to store in MySQL
body: <p>Good afternoon, </p>
  
  <p>I have searched for an answer to this query and the closest I could find was this:  <a href="https://stackoverflow.com/questions/15911382/storing-elements-in-an-array-at-each-iteration-of-a-foreach-on-php">Storing elements in an array at each iteration of a foreach on PHP</a></p>
  
  <p>I am building my own eCommerce platform using PHP and I have gotten to the point now where I can add items to my cart. </p>
  
  <p>I am currently building the checkout page but I dont know how to store each product ordered into different variables so that I can store them into MySQL. </p>
  
  <p>The following code allows me to populate the relevant data I just dont know how to store this data into the variables:</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-html lang-html prettyprint-override"><code>&lt;?php 
  session_start();
  
  include '../connection.php';
  
   $cartProducts = array();
   
   foreach ($_SESSION["cart_item"] as $item){
   
    
      
    echo "Product Details Acquired:" . "&lt;br&gt;";       
    echo $item["product_name"] . "&lt;br&gt;"; 
    echo $item["quantity"] . "&lt;br&gt;";
    echo "£".$item["product_price"] . "&lt;br&gt; &lt;br&gt;";
      
    } 
  
  
   //setting username variable
   $myusername = $_SESSION['login_user'];
  
   // getting client info
   include '../connection.php';
    $sql="SELECT id, username, phone, email from clients WHERE username='$myusername'";
     if ($result = mysqli_query($connection, $sql)){
      //Presenting data from array  
      while ($row = mysqli_fetch_array($result))  {
        $client_id= $row['id'];
        $client_phone= $row['phone'];
        $client_user= $row['username'];
        $client_email= $row['email'];
        
        echo "User details acquired" ."&lt;br&gt;";
        echo $client_id ."&lt;br&gt;";
        echo $client_phone ."&lt;br&gt;";
        echo $client_user ."&lt;br&gt;";
        echo $client_email ."&lt;br&gt;";
       }
      }
      else {
        echo "No client data found...";
       }
    
   
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This produces all of the data i need:</p>
  
  <p>Product Details Acquired:
  Chair
  1
  £129.89</p>
  
  <p>Product Details Acquired:
  Double Bed
  1
  £1999</p>
  
  <p>User details acquired
  4
  2147483647
  coreyhowe12
  corey@test.com</p>
  
  <p>Would appreciate any help :) </p>