There might be an easy answer to this, but I can’t for the life of me get this to work.
I’m using PHP and MySQL and have something like this set up:
$studentname = mysql_real_escape_string($_POST['sname']);
$studentnumber = mysql_real_escape_string($_POST['snumber']);
$course = mysql_real_escape_string($_POST['courseselect']);
$bike3 = mysql_query("SELECT stoodnumber FROM bikes505 WHERE stoodname='" . $studentname . "'");
$bikestoods = mysql_fetch_array($bike3);
while($row = mysql_fetch_array($stoodlistq))
{
$stoodentname = $row['stoodname'];
$stoodentnumber = $row['stoodnumber'];
//$coursename = $row2['coursename'];
//$coursecode = $row2['coursecode'];
if($studentname == $stoodentname && $studentnumber == $stoodentnumber){
//$success = 1;
//$success = "yupp";
//echo "SUCCESS, WE CAN REGISTER YOOOU!";
echo "<br>";
//echo "INSERT INTO " . $course . "";
switch($course){
case "Biking Safely":
if($studentnumber = $bikestoods[0] or $bikestoods[1]){
echo "Sorry, this student has already registered";
} else if($bikecurrent < $bikemax){
mysql_query("INSERT INTO bikes505 VALUES ('" . $stoodentname . "','" . $stoodentnumber . "')");
echo "Yay, successfully registered " . $stoodentname . " - " . $stoodentnumber . " for " . $course;
echo "<br>";
} else{
echo "Sorry, class is full!";
}
break;
…and so on. The only problem that I’m having is that if I have two students with the same name, the second in the list will echo that the information is not correct.
For example, the MySQL table has ‘stoodname’ and ‘stoodnumber’, and if ‘Jimmy St.James’,’1010′ and ‘Jimmy St.James’,’1090′ are both records in the table it will only let me enroll Jimmy 1010 in the course and not Jimmy 1090.
Am I just way off with how I’m validating? Or am I missing something really obvious? At first I assumed it was just because I was only using the first item in the array $bikestoods[0] so I changed it to $bikestoods[0] or $bikestoods[1] and it still doesn’t work.
Per a comment, you have
$stoodlistqdefined as:Later, you use the following block of code:
The
while()loop in this code iterates over every record, however, theif-statementchecks against the POST variables. The POST variables make up a single combination for “one student” – therefore, you’ll only ever get a single student that can be enrolled.To fix the issue, you’ll either need to update your form that submits the “student name”/”student number” to accept multiple inputs – or update the
if-statementto only check against the student’s name (which could lead to a more undesired situation).