I am trying to get a completed order to save to a table called orders in a database. In the database, the orders table only shows the last product, but I need it to show all the products on the order.
In the code below ($query2) it grabs all the ids of the products that are in the users cart.(There are two products in the cart)
I then use a while loop to display them, but if I try to INSERT them into the orders table($query3) using the $pro_id variable, it only grabs the last product in the cart and enters it into the database.
How do I get both products to enter into the orders table? Do I need to use an array somehow? Or is there a different way to do this?
Thanks!
$ip = getIp();
$query = "SELECT * FROM cart WHERE ip='$ip'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$pro_qty = $row['quantity'];
$query2 = "SELECT * FROM product WHERE id='$id'";
$result2 = mysqli_query($conn, $query2);
if(mysqli_num_rows($result2) > 0){
while ($row = mysqli_fetch_array($result2)){
$pro_id = $row['id'];
}
}
}
}
if(isset($_POST['placeOrder'])){
$query3 = "INSERT INTO orders (productId) VALUES ('$pro_id')";
$result3 = mysqli_query($conn, $query3);
}
}