2013-02-28 84 views
0

如何通过使用相同的ID通过foreach循环发送多个字段值?如何通过foreach循环发送多个fileds值?

此HTML代码有什么问题?

<input type="text" size="3" name="new_quantity[<?php echo $product_id; ?>]" value="<?php echo $product_quantity; ?>" /> 
<input type="text" name="totalcost[<?php echo $product_id; ?>]" value="<?php $total_cost; ?>" /> 

现在PHP代码

foreach($_REQUEST['new_quantity'] as $key => $quantity) { 
    foreach($_REQUEST['totalcost'] as $key => $total) { 
     $sql=mysql_query("UPDATE `categories`.`carts` SET `product_quantity` = '$quantity',    `product_totalcost` = '$total' WHERE `carts`.`product_id` ='$key' AND cart_session='$sessionID'"); 
    } 
} 

回答

0

循环回路内会给你N * N次更新查询

但你确实需要更新查询n次

这样做像这样:

$pids  = array_keys($_REQUEST['new_quantity']); /// here you will get all product_ids 

for($i=0; $i<count($_REQUEST['new_quantity']);$i++){ 
    echo $_REQUEST['new_quantity'][$pids[$i]]; // this quantity 
    echo $_REQUEST['totalcost'][$pids[$i]]; // total cost 
    echo $pids[$i]; // product id 

    /// add your update query here all details you need in query are echoed above. 
} 
+0

编辑的answe,请检查现在 – 2013-02-28 12:40:33

-1

尝试做一件事做一个数组,一个用于成本

$qt=array(); 
foreach($_REQUEST['new_quantity'] as $key => $quantity) { 
$qt[].=$quantity; 
} 

$ct=array(); 
foreach($_REQUEST['totalcost'] as $key => $total) { 
$ct[].=$total; 
} 

随后击发更新查询在for循环

+0

为什么你需要'.''$ qt []。'???! – 2013-02-28 12:41:29

+0

所有的值都会在数组中追加索引 – KAsh 2013-02-28 12:42:53

+0

对于'$ qt [] = $ quantity,你不需要'.';'这将工作 – 2013-02-28 12:44:19