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
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.

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.