将输入限制为sql表的某个数字

I have a SQL database within which I have a series of different tables. Basically, I am creating an exam day and adding exams to it.

Example:

(examtable) Exam 1

(exams) Physics, Biology, Chemistry, P.E

These both are linked.

Now currently how this works is when I create an exam in the examtable, I can add an unlimited amount of exams to it, but in real life that obviously does not make sense.

So what I am trying to do is have some sort of input when creating the exam that can limit the amount of exams that can be entered to it.

  • So for example if I create Exam 2 and limit input to 3, then only 3 exams can be added to it.

I have tried to find something online but cannot seem to get anywhere, could someone please help?

Thanks.

I don't understand very well your question but here it goes:

You can use sql in php to count the number of exams from examstable: 'SELECT COUNT(*) FROM examstable;' And then in PHP : 'if ($no_exams > 3 ) { echo "No more exams can be added."} else insert into the database the exam.'

Add another column to your table called mostExams

when creating a new exam just,

$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

$stmnt = $db->query("SELECT `mostExams` FROM `exams` WHERE `classId`='" . $thisClassId . "'");

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $mostExams = $row['mostExams'] ;
}

//php to only allow $mostExams number of exams.

$stmnt = $db->query("SELECT `id` FROM `exams` WHERE `classId`='" . $thisClassId . "'");
if ($stmnt->rowCount() >= $mostExams){
    echo 'You cannot add any more exams.  Change mostExams in the SQL database to allow more.';
}else{
    //HTML form to add an exam

}

You may want to add some code to check and see if you already have some exams and subtract that from most exams. You just need to think outside the box a little bit!