Well, depending on the details of your alternate way it seems like it should work, but without seeing the full code I can't say.
I still think you're best to collect the student first, and then base the code off that student's details rather than the way you have it, or the other way you tried.
does what I said not make sense?
Also, looking further at your code:
a student has a classroom, and can have multiple courses.
this means that if you change their classroom for one course, you change it for all, I'm assuming this is intended?
anyhows, reformatted some of your code a lil, but mebbe this'll help:
<html>
<body>
<center>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr><td>Student ID:</td><td><input type="text" name="studentNum"/></td></tr>
<tr><td>FirstName:</td><td><input type="text" name="firstName"/></td></tr>
<tr><td>LastName:</td><td><input type="text" name="lastName"/></td></tr>
<tr><td>Class Room number:</td>
<td>
<SELECT NAME="ClassRoom">
<OPTION VALUE="1">1</OPTION>
<OPTION VALUE="2">2</OPTION>
<OPTION VALUE="3">3</OPTION>
<OPTION VALUE="4">4</OPTION>
<OPTION VALUE="5">5</OPTION>
</SELECT>
</td></tr>
<tr><td>Course ID:</td><td><input type="text" name="courseID"/></td></tr>
<tr><td colspan="2" align="centre"><input type="submit" value="SUBMIT"/></td></tr>
</table>
</form>
<?php
// Connect to the database server
$dbcnx = @mysql_connect('xxxx', 'xxxx', 'xxxx');
if (!$dbcnx) {
exit('<p>Could not connect to the database server.</p>');
}//end of if-statement
// Select the database
if (!@mysql_select_db('database_Name')) {
exit('<p>Cannot locate the database_Name database.</p>');
}//end of if-statement
//Traverse through the COURSE_ALLOCATION table and count the number of entries for
//each user id, aka: how many students per course.
$result = mysql_query('SELECT courseID, courseTotalCapacity FROM course WHERE courseID IN (101, 102,103)') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
$course_array[$row['courseID']]['courseTotalCapacity']] = $row['courseTotalCapacity'];
}//end of while-loop
// collect posted information.
$studentNum = $_POST['studentNum'];
$firtName = $_POST['firtName'];
$lastName = $_POST['lastName'];
$ClassRoom = $_POST['ClassRoom'];
$courseID = $_POST['courseID'];
// removing matches for the current student - would also limit to current course but seems unnecessary.
// $result = mysql_query('SELECT courseID, COUNT(*) AS count FROM course_allocation WHERE courseID IN (101, 102,103) GROUP BY courseID') or exit(mysql_error());
$query = 'SELECT courseID, COUNT(*) AS count ';
$query .= 'FROM course_allocation ';
$query .= 'WHERE courseID IN (101, 102,103) ';
// adding next line:
$query .= 'AND studentNum != '.intval( $studentNum ).' ';
$query .= 'GROUP BY courseID';
$result = mysql_query( $query ) or die( mysql_error() );
while ($row = mysql_fetch_assoc($result))
{
$course_array[$row['courseID']]['count']] = $row['count'];
}//end of while-loop
echo "<br />";
if($course_array[$courseID]['count']<$course_array[$courseID]['courseTotalCapacity'])
{
$sql = "UPDATE student SET
studentNum='$studentNum',
firstName='$firstName',
lastName='$lastName',
ClassRoom='$ClassRoom'";
if (@mysql_query($sql)) {
echo '<p>Submitted student has been added.</p>';
} else {
echo '<p>Error adding submitted student: ' .
mysql_error() . '</p>';
}
$sql1 = "UPDATE course_allocation SET
studentNum='$studentNum',
courseID='$courseID'";
if (@mysql_query($sql1)) {
echo '<p>Submitted student has been allocated.</p>';
} else {
echo '<p>Error allocating submitted student: ' .
mysql_error() . '</p>';
}
}//end of if-statement
else
{
echo 'sorry. you have reached the limit for course #' . $key;
echo "<br />";
}//end of else
?>
</center>
</body>
</html>