简体   繁体   English

PHP重复执行SQL输入X次

[英]PHP repeat SQL entry X amount of times

I have an ecommerce store I am setting up and I need every item purchased entered in the database. 我正在建立一家电子商务商店,我需要在数据库中输入的所有已购买商品。 For example, someone purchases a qty of 2, I need to repeat the SQL data entry so it has two entries. 例如,某人购买的数量为2,我需要重复SQL数据输入,所以它有两个输入。 See code below and let me know how I can use product_qty to achieve this. 请参见下面的代码,让我知道如何使用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());
    }

}

You could do something like this? 你可以做这样的事情吗?

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')") 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM