2016-08-17 134 views
0

我有一個電子商務商店,我正在建立,我需要購買的每件商品都是在數據庫中輸入的。例如,某人購買了2的數量,我需要重複SQL數據輸入,因此它有兩個條目。看下面的代碼,讓我知道我可以如何使用product_qty來實現這一點。PHP重複SQL條目X次數量

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()); 
    } 

} 
+0

你很容易受到[SQL注入攻擊(http://bobby-tables.com) –

+0

是的,我知道,這是發展初期,將被添加在一次我有一個正常運作的代碼。 – user2044644

回答

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

謝謝!你把我放在正確的軌道上 - 只需要一個變化$我必須等於1而不是零 – user2044644