dqs66973 2016-08-17 16:53
浏览 14
已采纳

PHP重复SQL条目X次数

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. See code below and let me know how I can use product_qty to achieve this.

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条回答 默认 最新

  • dswqz24846 2016-08-17 16:56
    关注

    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')") 
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作