Jump to content

Foreach - Invalid arguement


ReeceSayer

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/248521-foreach-invalid-arguement/
Share on other sites

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.

 

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)

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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.