2013-12-24 88 views
-2

我有一个用foreach()更新数量的购物车。PHP中的无效参数foreach()

HTML

<form method="post" > 
<?php while ($data2 = mysql_fetch_array($data)) { ?> 
<tr> 
    <td> <input name="quantity" type="text" class="form-field" size="3" value="<? echo $data2['quantity'];?>" /> 
    <input type="hidden" name="product_id" value="<? echo $data2['product_id']; ?>"> 
    </td> 
    </tr> 
    <? } ?> 
    </form> 

和我的PHP,我有

if (isset($_POST['submit_qty'])) 
{ 
foreach($_POST['product_id'] as $key => $id) 
{ 
    $item_id = $id; 
    $quantity = $_POST['quantity'][$key]; 
    $sql2 = "update cart SET quantity = '".$quantity."' where product_id = '".$item_id."' "; 
    $result2 = mysql_query($sql2) or die ("Error in query: $result2"); 
} 

header('Location: mycart.php'); 
exit(); 
} 

如果我赞同我的SQL查询,我得到:

警告:的foreach提供了无效的参数()

可能是什么问题?

+3

'$ _POST ['product_id']'不是数组。 –

+2

这个问题已被问过1000次以上,并且回答了更多次。下一次请在提问前使用搜索;它很可能已经被回答了。 –

+0

[为foreach()提供的无效参数可能重复(http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Roopendra

回答

2

您没有将您的product_id作为数组传递。

尝试<input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">