使用会话购物车

时间:2017-11-19 21:58:27

标签: php mysql

如何使用单个表格上传多个产品名称?
(这是我当前的sql更新,它工作单个而不是多个):

$query = "INSERT INTO order_details(product_name) VALUES('$product_name')";

这是我使用会话的示例输入表单(已经删除了一些行,因为它太长了):

       <?php
    	if(isset($_SESSION["cart_products"])) //check session var
        {
    		$total = 0; //set initial total value
    		$b = 0; //var for zebra stripe table 
    		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_color = $cart_itm["product_color"];
    			$subtotal = ($product_price * $product_qty); //calculate Price x Qty
    			
    		   	$bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe 
    echo '<td width="5%"><input type="checkbox" class="inp_check " name="remove_code[]" value="'.$product_code.'" />
      </section>
    </td>';
    echo '<td width="40%" align="left"><input type="hidden" name="product_name" value="'.$product_name.'">'.$product_name.'</td>';
    echo '<td width="5%" align="center"><input type="number" size="2" class="ip_remove" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /> Unit</td>'

我想要做的是当人们点击按钮提交它应该上传sql就像一次更新

    id    order_id    product_name     product_price
    1     14           iphone 7        USD700  
    1     14           iphone 7s       USD700     
    1     14           iphone 5        USD700

    2     15           iphone 7       USD700
    2     15           iphone 7s      USD700
    2     15           iphone 5      USD700

或此示例照片 enter image description here

1 个答案:

答案 0 :(得分:0)

sql中的

values可以接受多个条目,例如:

INSERT INTO order_details(product_name) 
VALUES
    ('product1'),
    ('producut2'),
    ('product3');