如何防止在php / mysql中具有唯一id的特定用户的重复输入

I am working on result management system for secondary school.

I don't want to enter the same subject for a particular user using his/her registration number as unique id. i.e. if I enter maths for John, i must not enter maths again for John.

Below is my code:

<?php
    $msg = "";

    $connection = mysqli_connect("localhost", "root", "", "result_mg_db");
    if (isset($_POST['submit'])) {
        $regno = addslashes(strip_tags($_POST['regno'])); 
        $subj = addslashes(strip_tags($_POST['subject'])); 
        $examscore = addslashes(strip_tags($_POST['examscore'])); 
        $cascore = addslashes(strip_tags($_POST['cascore']));
        $date = date("Y-m-d h:i:sa"); 

        $confirm_subj = mysqli_query($connection, "SELECT subject FROM jss1_resulttbl WHERE regno = '$regno'");

        if ($confirm_subj == $_POST['subject']) {
            $msg = "This subject has been registered";
        } else {
            if ($cascore == ""|| $examscore == ""|| $subj == "" || $regno == "" ) {
                $msg = "Please fill all fields";
            } else {
                $reg_subj = mysqli_query($connection, "INSERT INTO jss1_resulttbl VALUES ('', '$regno', '$subj', '$examscore', '$cascore', '$date')");
                if (!$reg_subj) {
                    echo "Subject Registration failed ";
                }else {
                    $msg = "You have Successfully entered score for " . $subj . "<br/>";
                } 
            }

        }
    } 
?>

Run a query first to check if database has already has a row with the registration number and subject. Then insert data to database.

$result = mysqli_query("SELECT * FROM jss1_resulttbl WHERE regno='$regno' AND subject='$subject'");
$num_rows = mysql_num_rows($result);

if (!$num_rows) {
   // Here write query to insert data to database 
}