循环中的复选框 - PHP

I'm trying to use checkboxes to add users to a specific group but I'm having trouble figuring exactly how the logic works. The list of users is pulled from a sql statement and is echo'd. I would like to display a checkbox beside each name, and if the box is checked that user will be added to the list using implode so I can store the members in one column.

Here is the output that displays a list containing users, their location, and a checkbox. I'd like to be able to select the checkbox and then add them to the group based on their userID($friendNum).

Update: the checkboxes are working and properly entering into the databse but the inputs in the first form aren't being entered. (title, description, date, location)

  $myUsername = $_SESSION['userid'];       
        if(isset($myUsername)){
            echo '<table><form method="post" action="event.php" name="groupInvite"> 
                <tr><td> Event Title:</td><td> <input type="text" name="eventTitle" /></td></tr>
                <tr><td>Event Description:</td><td> <textarea name="eventDescription" /></textarea></td></tr>
                <tr><td>Event Date: </td><td><input type="text" name="eventDate" /></td></tr>
                <tr><td>Event Location:</td><td><input type="text" name="eventLocation" /></td></tr></table>
                <table><tr><td>Username </td><td> Location</td><td>Invite To Event</td></tr>'; 

        $friends = mysql_query("SELECT userid1 as friendId FROM friends WHERE userid2 = $myUsername AND friendstatus = 1
                                UNION SELECT userid2 as friendId FROM friends WHERE userid1 = $myUsername AND friendstatus = 1");

            while($friend = mysql_fetch_array($friends)){
                $userID = $friend['friendId'];
                $friendNum = mysql_query("select * from users where userid = $userID");
                $friendID = mysql_fetch_array($friendNum);
                $userLocation = mysql_query("select * from userinfo where userid='$userID'");
                $locationResult = mysql_fetch_array($userLocation);
                $locationResultArray = $locationResult['userlocation'];
                $locationExplode = explode("~","$locationResultArray");

        echo '<tr>
                <td><a href="profile.php?userid=' . $friendID['userid'] . '">' . $friendID['username'] .  '</a></td>
                <td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>
                <td><input type="checkbox" name="friendID[]" value='.$friendID['userid'].' /></td></tr>';
                }   
            echo '<tr><td> <input type="submit" name="eventSubmit" value="Create Event" />
            </td></tr>
            </form></table>';     
    }   

      if(isset($_POST['eventSubmit'])){
            $userList = implode("~",$_POST['friendID']);
            $sql = "INSERT INTO events VALUES('$userList',...)";  
             $result = mysql_query($sql);   
            echo "Event Created"; //just used for testing purposes
            }  
    }                       

There are many things wrong with this.

  1. All input elements have to be inside the <form>. i.e., open your form before you start outputting the checkboxes. It might work in some browsers regardless, but not all. I ran into a problem like this recently where only the data in the form would be submitted, but this only occured in Firefox.
  2. Set the value of each checkbox to the friend ID; there's no need to store it in a hidden element and then try to match it up later. The name of every textbox should be the same with [] appended to it. e.g., <input type="checkbox" value="428" name="friends[]" />. This will cause PHP to organize the $_POST data into an array for you so that you can easily loop over it.
  3. Don't store all the friend IDs in a single column in the DB. It will make it a nightmare to query later. Use a many-to-many table (Google it if uncertain).
  4. It's not really an error, but your HTML structure could use some more work too. A better design might look like:

example:

<ul>
    <li><label><input type="checkbox" value="428" name="friends[]" /> John Smith</label></li>
    <li><label><input type="checkbox" value="235" name="friends[]" /> Jane Doe</label></li>
</ul>

The labels will make the checkbox easier to click (can click anywhere on the label), and I believe it makes it easier for screen readers too. A <ul> is more appropriate here semantically, because its a list of friends you're trying to output.

Set the checkboxes as an array

HTML

<input type="checkbox" value="1" name="myVariable[]" />
<input type="checkbox" value="2" name="myVariable[]" />

PHP (on post check)

$stringToInsert = implode("~",$_POST['myVariable']);

Result if both are checked

1~2