I am trying to do a shopping add to cart webpage with reference from this source code I found online which is using pdo.
This is what I have tried to converting to mysqli, but It's still not working.
// Check the session variable for products in cart
$products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$products = array();
$subtotal = 0.00;
// If there are products in cart
if ($products_in_cart) {
// There are products in the cart so we need to select those products from the database
// Products in cart array to question mark string array, we need the SQL statement to include IN (?,?,?,...etc)
$array_to_question_marks = implode(',', array_fill(0, count($products_in_cart), '?'));
$stmt = $con->prepare('SELECT * FROM products WHERE product_id IN (' . $array_to_question_marks . ')');
$arrayKeys = array_keys($products_in_cart);
$stmt->bind_param(str_repeat("s", count($arrayKeys)), ...$arrayKeys);
$stmt->execute();
// Calculate the subtotal
foreach ($products as $product) {
$subtotal += (float)$product['p_price'] * (int)$products_in_cart[$product['product_id']];
}
}