How to check 2 student numbers in my database?

I have the PHP below and it works fine. But this form has room for 2 student numbers and I want to check both.

I tried just duplicating the code below, changing the $stmt1 $stmt2, but I just get my $_SESSION[‘error’] message:

$_SESSION[‘error’] = ‘Step 2.2 Check the SN: This student number: ’ . $studentnr2 . ’ is not in the 20BE classes students’ database. Please try again!’;

Also, $studentnr2 is not displayed. I checked and $studentnr2 is passed to PHP and appears in the text file accompanying the file, and in the text message which PHP writes to a text file.

I also tried calling $pdo $pdo1 and $pdo2, but PHP will not accept that. Is $pdo somehow reserved??
I thought I could call it anything!

Like I said, the code below works fine, and PHP continues and uploads the file I want with the message containing both student numbers, names and email.

How can I integrate the second student number in the checking process??

include '../../includes/studentdbWriteto.inc.php';
		//check if the studentnr exists in this course names and numbers list
		$stmt1 = $pdo->prepare('SELECT 1 FROM allstudents20BE WHERE studentnr = ?');
		try{
			$stmt1->execute([$studentnr1]);
			//echo 'row count is ' . $stmt1->rowCount();
				if($stmt1->rowCount() == 0){
					//echo 'this student is not in the database';
				$_SESSION['error'] = 'Step 2 Check the SN: This student number: ' . $studentnr1 . ' is not in the 20BE classes students\' database. Please try again!';
				//echo $_SESSION['error'];
				header('Location: /uploadFiles20BE/uploadessays_pairs_V1.html.php');
				exit();
				// if the student number exists, carry on, save the answers			
			}
		}
			catch(PDOException $e){
			$_SESSION['error'] = $e->getMessage();
			//echo $_SESSION['error'];			
			header('Location: /uploadFiles20BE/uploadessays_pairs_V1.html.php');
			exit();
		}

First, I don’t know why you are using include as the whole PHP code should be there? It would make life easier for you and the person trying to help you. I really don’t understand what you mean by 2 student numbers? Are the two different students or does the same student have 2 numbers? I take it that it’s two different students and there might be more?

The first thing I would do is check out this link → (The only proper) PDO tutorial - Treating PHP Delusions as it will help you understand PDO better. There is really no need to use try-catch for what you are doing with this code. I would also have the query string separated from the prepared method call -

$sql = 'SELECT 1 FROM all_students WHERE student_number = ?';
$stmt = $pdo->prepared($sql);

It makes it easier to read and to debug, plus I think :thinking: you need to restructure your database table a little bit. There’s no to have two separate tables (that is what it looks like you are doing?) as all you have to do is if the table didn’t successfully update then just insert the new student’s data into the table (after making sure the student doesn’t exist) and off you go. Usually you will have some kind of registration where you don’t have to check if the user exists, so most of the time it’s a mute point. Like I said, look at that link as I think it will help you out tremendously. Well, I kind of take it back with only having one database table, but it should be separated at first, by that the student information in one table and the grades for the test(s) in another table then you can simply join them if you need to.

Solved: in the first if () I wrote only 1 “=” for $studentnr2, that caused my problem, it set $studentnr2 = ‘’ !!! Works OK now!

Thanks for your reply. Let me explain a bit, make things clearer.

“include” at the top above is just the “connect to the database details”, db, user pw.

Actually, in this case, I am not saving any data to a table. The file is uploaded, and the student details and email are saved to a text file with the ending .data in the same folder.

I have an html form which students can use to upload a file to my webpage. The form checks their student number, to make sure it is in the allstudents20BE table. If the student number is in the table the upload proceeds, otherwise they bounce back to the upload form. Works well.

This coming term I have an Oral English class. I often give them the task of making a dialogue with a partner about some topic, recording their dialogue and sending it to me as homework. I download it, listen to it and give them a score.

For that I need 2 student numbers. Both must be in the allstudents20BE table. Somehow, I am having trouble getting PHP to check the second student number. I don’t know why!

In principle, if the check on the first student number works, it should work on the second student number!

Sponsor our Newsletter | Privacy Policy | Terms of Service