Jump to content

Checkboxes to multiple SQL rows


Call-911

Recommended Posts

Hey All,

 

I tried seraching, but I'm not sure what exactly to search for. Here's what I need to do, and any push in the right direction would be great:

 

I have a PHP form, with the Following:

 

Text Box: Student ID

Checkboxes for Activities

 

What I need to do is somehow loop through and make a new row for each activity. Example, if I entered Student ID 105 and Activities are Basketball, Baseball, and Chess Team, I need three different rows in the database each listing the individual activity and the student ID.

 

Any suggestions? I know how to do this if it was one entry (radio buttons) but want to be able to enter multiple rows at a time. Thanks!

Link to comment
Share on other sites

Instead of creating table fields for each type of activity as those will change, I would just have StudentID and Activities.  Then for each posted (checkbox) activity insert studentID and the activity.  You'd have something like this in your table assuming id is 5.

StudentID | Activities

5              | Basketball

5              | Baseball

5              | Chess Team

 

Then to show records you query for for student id and it would pull up all names listed under Activities where the student id matches.  If you wanted to show all all students that for example are in Basketball, you would search for that.

Link to comment
Share on other sites

That's exactly what I want to do, but to do it with Checkboxes. I know how to do that with dropdown menus or radio boxes, but how can I make a check box so they can go through and say "ID 5" has "Basketball", "Baseball", and "Chess Team" and to have it turn out:

 

StudentID | Activities

5              | Basketball

5              | Baseball

5              | Chess Team

 

 

Link to comment
Share on other sites

This sample page shows two options for using checkboxes.  The big difference is in whether you use a form for each student or process all students at once.  In this sample I used arrays for testing and demonstrating and pull out the values using a "foreach" statement.  You will replace these foreach statements with mysql queries.  Sample queries and inserts into DB are provided (commented out).

<?php
//Just for testing I'm creating arrays to hold student id and name.  I'm also creating an array to hold activities.  
//It's expected that these will be removed as well as the foreach statement shown in the forms below and replaced with mysql queries where the foreach code is.
$StudentIDs=array(5 => "Mark Flinn", 7 => "Mary Smith", 8 => "Robert Moss", 11 => "Jimmy Griffen", 12 => "Hanna Major");
$activities=array("Basketball", "Baseball", "Chess Team", "Volleyball", "Football");
//using this count in the lower form.   
$arraycount=count($activities);  
$arraycount2=count($activities)+1;

//Process top form that posts each user individually 
if(isset($_POST['submit'])){
$studentID=$_POST['stuID'];
foreach($_POST as $key => $value){
	if($value=="yes"){
//echo values for testing
echo "<br />$studentID $key";
/*Sample insert
$studentID = mysql_real_escape_string($studentID);
$key = mysql_real_escape_string($key);
mysql_query("INSERT INTO `stuactivities` SET studentID='$studentID', activity='$key'");*/
	}
}
}
///*******///
//Process bottom form that posts all users at the same time
if(isset($_POST['submit2'])){
foreach($_POST as $key => $value){
	if($value!="Submit"){
		foreach($value as $studentID => $value2){
//echo values for testing
echo "<br />$studentID $key";
/*Sample insert
$studentID = mysql_real_escape_string($studentID);
$key = mysql_real_escape_string($key);
mysql_query("INSERT INTO `stuactivities` SET studentID='$studentID', activity='$key'");*/
		}
	}
}
}

//For testing I'm using foreach statements to pull values from the above arrays.  Replace each foreach statement below with your mysql query
?>

<html>
<body>
<?php
foreach ($StudentIDs as $stuID => $stuname){
/*Sample query
$getnames = mysql_query("SELECT name,studentID FROM students");
WHILE($userinfo = mysql_fetch_array($getnames)){
$stuID=$userinfo['studentID'];
$stuname=$userinfo['name'];
*/
?>
<table border=0 summary="" cellpadding="0" style="background:#E9E9E9;">
<tr>
<td>
<form method="post" action="" style="padding:0; margin:0">
<div style="width:150px;float:left">Student: <?php echo "$stuname";?>
<input type="hidden" name="stuID" value="<?php echo "$stuID";?>" /></div>
<?PHP 
foreach ($activities as $activity){
/*Sample query
$getactivities = mysql_query("SELECT activity FROM activities");
WHILE($gtactivities = mysql_fetch_array($getactivities)){
$activity=$gtactivities['activity'];
*/
echo "<div style=\"width:100px;float:left;text-align:center\">$activity <input type=\"checkbox\" name=\"$activity\" value=\"yes\" /></div>";
}
?>
<input type="submit" name="submit" value="Submit" />
</form></td>
</tr>
</table>
<?PHP
} 
?>
<br /><br />
<form method="post" action=""> 
<table border=0 summary="" cellpadding="3" style="background:#E9E9E9">
<tr>
<td>Student Name</td><td colspan="<?php echo "$arraycount";?>"  align="center">Activities</td></tr>
<?PHP
foreach ($StudentIDs as $stuID => $stuname){
/*Sample query
$getnames = mysql_query("SELECT name,studentID FROM students");
WHILE($userinfo = mysql_fetch_array($getnames)){
$stuID=$userinfo['studentID'];
$stuname=$userinfo['name'];
*/
?>
<tr>
	<td><?php echo "$stuname";?></td>		

<?PHP 
foreach ($activities as $activity){
/*Sample query
$getactivities = mysql_query("SELECT activity FROM activities");
WHILE($gtactivities = mysql_fetch_array($getactivities)){
$activity=$gtactivities['activity'];
*/
echo "<td>$activity";?> <input type="checkbox" name="<?php echo "$activity";?>[<?php echo "$stuID";?>]" value="yes" />
</td>
<?PHP
}
echo "</tr>";
}
?>
<tr>
<td colspan="<?php echo "$arraycount2";?>"><input type="submit" name="submit2" value="Submit" /></td>
</tr>
</table>
</form>

</body>
</html>

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.