This page should add a record to a database at each loop. However, when the loop reaches the $stmt->execute(), the loop stops. I know the opendb.php I include should be right, I've used it before.
<!DOCTYPE html>
<html lang="nl">
<head><link rel="stylesheet" type="text/css" href="style/style.css">
<title> Afronden Bestelling </title>
</head>
<body>
<div id="wrap">
<?php include_once ('includes/navigation.php'); ?>
<div id="main">
<br>
<br>
<h1> Ik ben leeg :( </h1>
<?php
// Database name
$dbname = "kledingwinkel";
$factuurid = 1;
$klantid = 42;
$test = 20;
// add record to database:
include("opendb.php");
$stmt = $db->prepare('INSERT INTO facturen ( productid, klantid, aantal, totaal, product_name ) VALUES ( ?, ?, ?, ?, ? )');
$stmt->bindValue(1, $test, PDO::PARAM_STR);
$stmt->bindValue(2, $klantid, PDO::PARAM_STR);
$stmt->bindValue(3, $product_qty, PDO::PARAM_STR);
$stmt->bindValue(4, $subtotal, PDO::PARAM_STR);
$stmt->bindValue(5, $product_name, PDO::PARAM_STR);
foreach ($_SESSION["cart_products"] as $cart_itm)
{
//set variables to use in content below
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_size = $cart_itm["product_size"];
$subtotal = ($product_price * $product_qty); //calculate Price x Qty
echo $product_name;
//echo $product_qty;
//echo $product_price;
//echo $product_code;
//echo $product_size;
//echo $subtotal;
$stmt->execute();
}
?>
<br>
<br>
</div>
</div>
<?php include_once ('includes/footer.php'); ?>
</body>
</html>
Is someone able to explain me what's going wrong? Thanks in advance.