I'm still working on a difficult registration program for a boy scout council. I'm working on a portion which registers 6 merit badge options for a list of scouts. I worked with a friends suggestions to create the code I have now. The problem I have is that I want to list the scouts names from another table within the form, so the scoutmaster can select a set of merit badges from each one. I have that part done. Now I'm having difficulty inserting the merit badge selections into a new table. I want to be able to insert the scouts name, troop number (which is also my SESSION_ID, so that's not really difficult) and the selections into a new table.
Here's what the code looks like:
[code=php]$query = "SELECT troop_num, council_name, campsite_name, week_number FROM users WHERE troop_num='" . $_SESSION['troop_num'] . "'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$tn = $row[0];
$cn = $row[1];
$csn = $row[2];
$w = $row[3];
if (!isset($_SESSION['troop_num'])) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop the slash
}
$url .= '/index.php';
ob_end_clean(); // Delete buffer
header("Location: $url");
exit(); // Quit
} else {
if (isset($_POST['submitted'])) {
for ($i = 1; $i < $counter; $i++) {
$sfn = 'scout_first' . $i;
$sln = 'scout_last' . $i;
$sa = 'scout_age' . $i;
$sr = 'scout_rank' . $i;
// Check for a scout first name.
if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST[$sfn])))) {
$sfn = escape_data($_POST[$sfn]);
} else {
$sfn = FALSE;
}
// Check for a scout last name.
if (eregi ('^[[:alpha:]\.\' \-]{2,30}$', stripslashes(trim($_POST[$sln])))) {
$sln = escape_data($_POST[$sln]);
} else {
$sln = FALSE;
}
// Check for a scout age.
if (eregi ('^([0-9]{2})?$', stripslashes(trim($_POST[$sa])))) {
$sa = escape_data($_POST[$sa]);
} else {
$sa = FALSE;
}
// Check for a scout rank.
if (eregi ('^[[:alpha:]\.\' \-]{2,20}$', stripslashes(trim($_POST[$sr])))) {
$sr = escape_data($_POST[$sr]);
} else {
$sr = FALSE;
}
if ($sfn == FALSE && $sln == TRUE && $sa == TRUE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s first name on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == FALSE && $sa == TRUE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s last name on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == TRUE && $sa == FALSE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s age on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == TRUE && $sa == TRUE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == TRUE && $sa == TRUE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s first name and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == FALSE && $sa == TRUE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s first and last name on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == TRUE && $sa == FALSE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s first name and age on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == FALSE && $sa == FALSE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s last name and age on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == FALSE && $sa == TRUE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s last name and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == TRUE && $sa == FALSE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s age and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == FALSE && $sa == FALSE && $sr == TRUE) {
echo '<b><font color="red">Please enter the scout\'s first and last name and age on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == FALSE && $sa == TRUE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s first and last name and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == TRUE && $sa == FALSE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s first name, age, and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == TRUE && $sln == FALSE && $sa == FALSE && $sr == FALSE) {
echo '<b><font color="red">Please enter the scout\'s last name, age, and rank on line '; echo $i; echo '</font></b><br />';
} else if ($sfn == FALSE && $sln == FALSE && $sa == FALSE && $sr == FALSE && $i == 5) {
echo '<b><font color="red">Please fill in the form if you wish to enter more scouts into your roster.</font></b>';
} else if ($sfn == FALSE && $sln == FALSE && $sa == FALSE && $sr == FALSE) {
} else {
// Add the scout
$query = "INSERT INTO roster (troop_num, council_name, campsite_name, scout_first, scout_last, scout_age, scout_rank) VALUES ('$tn', '$cn', '$csn', '$sfn', '$sln', '$sa', '$sr')";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if ($i == $counter) {
if ($result) { // If it ran OK.
// Finish the page.
echo '<b><font color="blue" size="+1">5 Scouts were successfully loaded into your roster.</font></b>';
} else { // If it did not run OK.
echo '<p><b><font color="red">Your roster could not be registered due to a system error. We apologize for any inconvenience.</font></b></p>';
}
}
}
} // End of FOR conditional
} // End of the main Submit conditional.
}
mysql_close(); // Close the database connection.
?>
<h2>Troop Roster</h2>
<p>Please now enter your troop roster here. All areas must be filled out for each scout. Do NOT use this area for your adult leaders.</p>
<form action="roster.php" method="post">
<fieldset>
<br />
<?php
echo '<p><b>Troop #: </b>'; echo $tn; echo '<b> Council Name: </b>'; echo $cn; echo '<b> Requested Campsite: </b>'; echo $csn; echo '<b> Week #: </b>'; echo $w;
for ($i = 1; $i < $counter; $i++) {
echo '<p><center><b>First Name:</b> <input type="text" name="scout_first' . $i . '" /> <b>Last Name:</b> <input type="text" name="scout_last' . $i . '" />
<b>Age:</b> <input type="text" name="scout_age' . $i . '" size="3" maxlength="2" /> <b>Rank:</b> <select name="scout_rank' . $i . '" />
<option></option>
<option>Scout</option>
<option>Tenderfoot</option>
<option>Second Class</option>
<option>First Class</option>
<option>Star</option>
<option>Life</option>
<option>Eagle</option>
</select>
</center></p><br />
';
}
?>
</fieldset>
<center>
<input type="submit" name="submit" value="Submit Scouts" />
<input type="button" name="next" onClick="view_roster.php" value="View Roster" />
</center>
<input type="hidden" name="submitted" value="TRUE" />
</form>[/code]
The form is at the bottom and calls the beginning of the php code for the submit.
Any suggestions are welcome, I have to present a working version of this code on Thursday, so I'm kind of rushed.
Thanks!