0

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']];
      }
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Tell us where you're stuck. I'm guessing you expect `$products` to be populated but it's not? Look over your code and consider how you've never set the variable anywhere, or even fetched the result set. – Ro Achterberg Nov 21 '20 at 09:51
  • Why would you want to convert from PDO to mysqli. PDO is much easier and much better. – Dharman Nov 21 '20 at 11:52

0 Answers0