I am trying to develop a store and am stuck due to my poor knowledge in PHP, I read online and try to implement with minimal understanding.
When I insert the cart items into MySQL so that I can generate an order number, each insert creates multiple ids by way of auto increment. What I want is one ID for all the products passed in that query so that I can take it to checkout page where I take user details. I have a foreach
loop, which helps me display products perfectly, but when I try using insert, it creates separate ids.
What I need is single order ID for each query, where temporary cart items can be passed onto checkout page and into MySQL
Following is my code:
<?php
session_start();
require_once("inc/connect.php");
?>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<?php
$data = '';
foreach ($_SESSION["cart_item"] as $item){
$product_id = $item["id"];
$quantity_per_product = $item["quantity"];
$data .= "(NULL, '$product_id', '$quantity_per_product', NULL),";
}
$data = rtrim($data, ',');
$ad="INSERT INTO `orders`(`order_id` ,`product_id` ,`quantity_per_product` ,`modified`)
VALUES $data;";
$q2 = mysqli_query($connection, $ad);
if (!$q2)
{
echo ('Q2 failed: ' . mysql_error());
}
else {echo 'success Q2';
echo "query: "; print_r($ad);}
$order_id=mysqli_insert_id($connection);
echo "<script language='javascript'>
window.location = 'checkout.php?checkout=$order_id';
</script>";
?>