ReeceSayer Posted October 5, 2011 Share Posted October 5, 2011 Hi, I've been looking at this for a couple of hours and it's killing me a little inside. I've butchered a few pieces of code from various sources together and come up with this. I understand how it's supposed to work (or i'm pretty sure i do) but the foreach statement is giving me the following error: Warning: Invalid argument supplied for foreach() /document/etc/etc on line 80 Obviously the offending statement is this one: <?php // Check if button name "Submit" is active, do this if($_SERVER['REQUEST_METHOD'] == "POST") { foreach($_POST['id'] as $id) { $update="UPDATE users SET firstname='" . $_POST["firstname".$id] . "', lastname='" . $_POST["lastname".$id] . "', email='" . $_POST["email".$id] . "', password='" . $_POST["password".$id] . "', adminid='" . $_POST["adminid".$id] . "' WHERE id='" . $id . "'"; $result = mysql_query($update); echo "Updated"; } } ?> Not a clue what the problem is atm. If someone could either tell me or point me in the right direction i'd appreciate it/ Reece Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/ Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 That means that $_POST['id'] is not an array. We would need to see your form to know, but it would need multiple id controls in the following format to be an array: <input name="id[]" ...> Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276266 Share on other sites More sharing options...
ReeceSayer Posted October 5, 2011 Author Share Posted October 5, 2011 Hi Shawn, Appreciate the quick reply - I'll post the rest of the code below: <section id="Results"> <CENTER> <?php $users = "SELECT * FROM users"; $listusers = mysql_query($users); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>First Name</strong></td> <td align="center"><strong>Last Name</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>Password</strong></td> <td align="center"><strong>Admin ID</strong></td> </tr> <?php while($rows=mysql_fetch_array($listusers)) { ?> <tr> <td align="center"><input type="hidden" name="id" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> <td align="center"><input name="First Name<? echo $rows['id']; ?>" type="text" id="firstname" value="<? echo $rows['firstname']; ?>"></td> <td align="center"><input name="Last Name<? echo $rows['id']; ?>" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td> <td align="center"><input name="Email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> <td align="center"><input name="Password<? echo $rows['id']; ?>" type="text" id="password" value="<? echo $rows['password']; ?>"></td> <td align="center"><input name="adminid<? echo $rows['id']; ?>"type="checkbox" id="adminid" value="1" <?php if ($rows['adminid'] ==1) { echo "checked";} else {} ?>></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"> <input type='hidden' name='Submit' /> <input type="Submit" name="Submit" value="Submit"></td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($_SERVER['REQUEST_METHOD'] == "POST") { foreach($_POST['id'] as $id) { $update="UPDATE users SET firstname='" . $_POST["firstname".$id] . "', lastname='" . $_POST["lastname".$id] . "', email='" . $_POST["email".$id] . "', password='" . $_POST["password".$id] . "', adminid='" . $_POST["adminid".$id] . "' WHERE id='" . $id . "'"; $result = mysql_query($update); echo "Updated"; } } ?> </CENTER> </section> There are a couple other things above it but thought i'd post just this to save you trawling through endless code. Cheers again for taking a look. Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276269 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 There's a better way, but I have limited time at the moment. This should work given what you have: Change to this (notice the [] ): <input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276280 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 Also, your name attributes do not match that of which you are trying to access in your $_POST. name="First Name1" cannot be accessed using $_POST['firstname1']; Edit the value of your name attribute to match your $_POST keys. (best solution) Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276283 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 Also, your name fields do not match that of which you are trying to access in your $_POST. name="First Name1" cannot be accessed using $_POST['firstname1']; Edit the value of your name attribute to match your $_POST keys. (best solution) Good catch. Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276285 Share on other sites More sharing options...
ReeceSayer Posted October 5, 2011 Author Share Posted October 5, 2011 Hi guys, It's running now with both of you're solutions in place. However, when submitting the form it wipes everything in the database except for the id (primary key). and i get an undefined index error on line 82 which is my foreach statement. Any ideas on this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276290 Share on other sites More sharing options...
ReeceSayer Posted October 6, 2011 Author Share Posted October 6, 2011 Hi again, Still having the same problem. Anyone got any ideas on this or could point me to a better way of doing it if there is one. Regards Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276549 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 That error, without seeing your current script, is I assume saying that $_POST['id'] does not exist. Try outputting your POST array to make sure its got everything you expect. echo '<pre>',print_r($_POST,true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276568 Share on other sites More sharing options...
ReeceSayer Posted October 7, 2011 Author Share Posted October 7, 2011 Hi Buddski, Brilliant suggestion. From being able to see the array i could see that the variables had the ID appended on the end. The issue is solved for now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/#findComment-1276826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.