I am having a bit of struggle with this. Hope some can guide me a little.
I am trying to take the shopping cart basket products which are in mysql(and not sessions in this case) and move them to an "order-details" table at the checkout.
The array of product is created ok and output the keys and values properly but my problem is when I try to insert the array values into the other table.
Here is the code:
$cart_products = array();
$stmt = $conn->prepare("SELECT
SUM(co.cart_quantity) AS quantity,
p.product_id,
p.product_name,
SUM(p.product_price) AS price,
p.short_description,
SUM(p.product_weight) AS weight,
p.vat
FROM cart_orders AS co
LEFT JOIN products AS p
ON co.cart_product_id = p.product_id
LEFT JOIN vat_rates AS vr
ON p.vat = vr.vat_id
WHERE cart_user_id = ?
GROUP BY co.cart_product_id
ORDER BY co.cart_product_id");
$stmt->bind_param('i', $user_id);
$stmt->bind_result($quantity,$p_product_id,$p_product_name,$price,$p_short_description,$weight,$p_vat);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() > 0){
while ($stmt->fetch()) {
$cart_products[] = array(
"product_id" => $p_product_id,
"product_name" => $p_product_name,
"product_quantity" => $quantity,
"product_price" => $price,
"vat_id" => $p_vat
);
}}
$stmt->free_result();
$stmt->close();
$stmt = $conn->prepare("INSERT INTO order_details(product_id,product_name,product_quantity,product_price,vat_id)
VALUES (?,?,?,?,?)");
$stmt->bind_param('isiii', $cart_products['product_id'],$cart_products['product_name'],$cart_products['product_quantity'],$cart_products['product_price'],$cart_products['vat_id']);
$stmt->execute();
$stmt->close();
This is the output of my array $cart_products:
Array
(
[0] => Array
(
[product_id] => 5
[product_name] => Product A
[product_quantity] => 20
[product_price] => 2.50
[vat_id] => 2
)
[1] => Array
(
[product_id] => 7
[product_name] => Product A
[product_quantity] => 10
[product_price] => 2.50
[vat_id] => 1
)
[2] => Array
(
[product_id] => 9
[product_name] => Product A44544
[product_quantity] => 3
[product_price] => 2.50
[vat_id] => 2
)
)
Thank you in advance for your help!