-2

I have a shopping cart with a foreach() to update the quantities.

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>

AND FOR MY PHP, I have

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

IF I Echo my sql query I get :

Warning: Invalid argument supplied for foreach()

what could be the problem ?

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
user3011784
  • 833
  • 1
  • 8
  • 30
  • 3
    `$_POST['product_id']` is not an array. – Justin Wood Dec 24 '13 at 02:35
  • 2
    This question has been asked about 1000+ times before, and answered even more times. Next time please use the search before asking a question; it is quite likely that it has already been answered. – Sverri M. Olsen Dec 24 '13 at 02:40
  • possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Roopendra Dec 24 '13 at 02:43

1 Answers1

2

You aren't passing your product_id as an array.

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

James Binford
  • 2,753
  • 1
  • 14
  • 12