View Full Version : error >>> Invalid argument supplied for foreach()

07-30-2012, 09:36 PM
I am working on a inventory control system and i want to edit user who assigned to stores. I user can be assigned to one or more stores. Iam using multiple seletc option.My problem here is in edit user page i wanna show user assigned to stores as selected. But when i am going to do that i got this error(Invalid argument supplied for foreach() on line 194 ) only when user is not assigned for all stores.
eg- say there is 3 stores called A, B and C
1.)if iam going to edit user who assigned to all three stores i won't get any error.
2.)If iam going to edit user who assigned to 2 stores I will get above error twice.
3.)If iam going to edit user who assigned to only one store I will get above error only once.

Pls help me to correct this

this is my code

$emp_id = $_GET['emp_id'];
$result1 = mysql_query("Select *
from members
JOIN store_employee on members.emp_id = store_employee.emp_id
JOIN stores on store_employee.store_id = stores.store_id
where members.emp_id=$emp_id")
or die(mysql_error());
$query="SELECT * FROM stores";
$dropdown = "<select name='store_id[]' multiple='multiple' style='height:80px;' size='5' id='store'>";
$result2 = mysql_query ($query);
while($row2 = mysql_fetch_array($result2))

$dropdown .= "\r\n<option value='{$row2['store_id']}'";

foreach(mysql_fetch_array($result1) as $row1) //this is line 194
$dropdown .=" selected='selected'";

$dropdown .= ">{$row2['store_name']}</option>";
$dropdown .= "\r\n</select>";
echo $dropdown;

07-30-2012, 10:00 PM
I have used
while ($rows = mysql_fetch_array($result1)) { instead of foreach but it doesnt work tooo :( but then it doesnt show all selected items.It shows only ane selected item. dats y i use this way to get results

07-31-2012, 12:21 AM
My understanding is that mysql_fetch_array won't restart once it has exhausted the rows.

You should perhaps consider storing the store names in an array, which you can then loop over repeatedly. There's some code samples here (http://uk3.php.net/manual/en/function.mysql-fetch-array.php).

I feel sure that you could do this with one query though.

I assume you have a good reason not to use mysqli :)