Php foreach循环在PDO执行时停止

时间:2016-01-29 12:07:45

标签: php mysql pdo

此页面应在每个循环中向数据库添加记录。 但是,当循环到达$ stmt-> execute()时,循环停止。 我知道我包含的opendb.php应该是对的,我以前用过它。

<!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>

有人能够解释我出了什么问题吗? 提前谢谢。

0 个答案:

没有答案