Radio Button in While Loop

This is a multi-part problem:

I am creating an RSVP page for my wedding website.
I have all the guests in a mySQL database.

The way I have it work is you go to the rsvp webpage, enter the “rsvp code” and all of those people linked with the rsvp code will show up.

Each person has a radio button.


Because I am gathering everyone who has the rsvp code in a while loop, to get the radio buttons to display with different names I had to give the radio button name a unique identifier and I based that off of the database id number.

Example: If 3 people identify with a given rsvp code, then using the code below,

<input type="radio" name="attending[COLOR="Red"]$id[/COLOR]" value="1">
<input type="radio" name="attending[COLOR="Red"]$id[/COLOR]" value="2">

will produce:

Person 1 “Radio buttons where id = 1”
Person 2 “Radio buttons where id = 2”
Person 3 “Radio buttons where id = 3”

Now each person has a unique radio button to RSVP to.

Upon pressing submit I’m having trouble passing everyone’s information into a mySQL UPDATE query.

Here is the website you can check it out. Type: 12345 as the code.

Here is the code.

echo "<form action=\\"rsvp/rsvp_updated.php\\" method=\\"post\\">";
echo "<table>";
echo "<tr> <th class=\\"la\\">Attending:</th> <th class=\\"la\\">Yes</th><th class=\\"la\\">No </tr>";
// keeps getting the next row until there are no more to get
while($user = mysql_fetch_array( $result )) {

  $fname = ($user['fname']);
  $lname = ($user['lname']);
  $email = ($user['email']);
  $phone = ($user['phone']);
  $address = ($user['address']);
  $city = ($user['city']);
  $state = ($user['state']);
  $zipcode = ($user['zipcode']);
  $attending = ($user['attending']);
  $guest_name = ($user['guest_name']);
  $code = ($user['code']);
  $id = ($user['id']);

	// Print out the contents of each row into a table
	echo "<tr><td class=\\"white_text\\">";
	echo "$fname $lname";
	echo "</td><td>";
	echo "<input align=\\"baseline\\" type=\\"radio\\" name=\\"attending$id\\" value=\\"1\\">";
	echo "</td><td>";
	echo "<input align=\\"baseline\\" type=\\"radio\\" name=\\"attending$id\\" value=\\"2\\">";
	echo "</td><td>";
	echo "<input name=\\"id\\" type=\\"hidden\\" value=\\"$id\\" />";
	echo "<input name=\\"fname\\" type=\\"hidden\\" value=\\"$fname\\" />";
	echo "<input name=\\"lname\\" type=\\"hidden\\" value=\\"$lname\\" />";
	echo "</td></tr>";

echo "</table>";
echo "<span class=\\"padding\\"><input name=\\"Update\\" type=\\"submit\\" value=\\"Update\\" /></span>";

Why not just change the value for the radio button to reflect the person attending? They can only select one option anyway and then you just update the entry for that person.

Also on another note this isn’t the best method as someone can accidentally mark someone else as attending…

I think what i’m going to do is create an extra script page for each individual person. The purpose of being able to change multiple people is that these people being invited would be part of a family, therefore it would be ok for the parents to speak for the children or one spouse to speak for another.

By putting each individual person into their own webpage I remove the problem of having to create unique names for each person’s radio button.

instead of name=“attending$id”, try name=“attending[$id]” instead
What this does is submit the checked radio boxes as an array.

For example when you submit, you can loop throught the chcked radio boxes like:

$checked = isset($_REQUEST['attending']) ? $_REQUEST['attending'] : false;
if($checked) {
foreach($checked as $user_id) {
$sql = "update user set ... where pk = $user_id";
} else {
// nothing was checked