1

Good afternoon,

I have searched for an answer to this query and the closest I could find was this: Storing elements in an array at each iteration of a foreach on PHP

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.

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.

The following code allows me to populate the relevant data I just dont know how to store this data into the variables:

<?php 
session_start();

include '../connection.php';

	$cartProducts = array();
	
	foreach ($_SESSION["cart_item"] as $item){
	
		
				
		echo "Product Details Acquired:" . "<br>";							
		echo $item["product_name"] . "<br>"; 
		echo $item["quantity"] . "<br>";
		echo "£".$item["product_price"] . "<br> <br>";
				
		} 


	//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" ."<br>";
						echo $client_id ."<br>";
						echo $client_phone ."<br>";
						echo $client_user ."<br>";
						echo $client_email ."<br>";
					}
				}
				else {
					 echo "No client data found...";
					}
		
	
?>

This produces all of the data i need:

Product Details Acquired: Chair 1 £129.89

Product Details Acquired: Double Bed 1 £1999

User details acquired 4 2147483647 coreyhowe12 [email protected]

Would appreciate any help :)

7
  • you can store the products in an array Commented May 1, 2017 at 12:28
  • I guess instead of echo-ing the product informations you want to store the values in variables to use later on. In that case you could store the values in an array. Commented May 1, 2017 at 12:28
  • Thanks guys, i guessed that this would be the case, how do i do this? Ive tried various code but had no luck... (code from the example question) Commented May 1, 2017 at 12:29
  • 1
    So you are basically asking how to write $product_name = $item["product_name"];? Commented May 1, 2017 at 12:31
  • Ok so what I want to do is: Foreach loop gets all products. Stores product 1 data into variables, then stores product 2 into variables (product id, name, price quantity). Then I want to store into MySQL (I know how to do this) I just need a hand populating the variables. Commented May 1, 2017 at 12:33

2 Answers 2

1

Why don't you use INSERT query in your foreach loop like this:

foreach ($_SESSION["cart_item"] as $item){

   // use your product attributes like this.
   $product_name = $item["product_name"];
   $quantity = $item["quantity"];

   // then use insert query like this
   $insert = "INSERT INTO products (`product_name`, .. other columns ..) VALUES ('$product_name', .. other column values ..)";
   mysqli_query($connection, $insert);

}
Sign up to request clarification or add additional context in comments.

Comments

0

Can you not store it in an array and then loop through the array for your insert query?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.