PHP重复SQL条目X次

时间:2016-08-17 16:53:32

标签: php

我有一个我正在设置的电子商务商店,我需要在数据库中输入的每件商品。例如,有人购买的数量为2,我需要重复SQL数据条目,因此它有两个条目。请参阅下面的代码,让我知道如何使用product_qty来实现这一目标。

if (isset($_SESSION["cart_products"]))
{
    $total = 0; //set initial total value
    $b = 0; //var for zebra stripe table 
    foreach ($_SESSION["cart_products"] as $cart_itm)
    {
        $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_color = $cart_itm["product_color"];
        $subtotal = ($product_price * $product_qty); //calculate Price x Qty
        $name = $_SESSION['firstName']. ' ' .$_SESSION['lastName']; 

        mysqli_query(
            $mysqli,
            "INSERT INTO Order_Tickets 
                    (sku, tktproduct, tktprice, purchaser)
             VALUES ('$product_code','$product_name','$product_price','$name')"
        ) or die(mysqli_error());
    }

}

1 个答案:

答案 0 :(得分:0)

你可以这样做吗?

if ($product_qty > 1)
{
    for ($i = 0; $i <= $product_qty; $i++)
    {
        mysqli_query($mysqli, "INSERT INTO Order_Tickets (sku, tktproduct, tktprice, purchaser) VALUES ('$product_code','$product_name','$product_price','$name')") 
    }
}