2013-02-28 80 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