I have a form where i save students login data to a database. The form includes the "admission_number", "username" and "password" fields. i want to show an error if the admission number is already existing and a user tries to add it again. Here's my php code for inserting the record.
<?php
        if(isset($_POST['submit']))
        {
        $server     = 'localhost';
        $username   = 'root';
        $password   = '';
        $course_code=$_POST['course_code'];
        $course_title=$_POST['course_title'];
        $course_units=$_POST['course_units'];
        $course_semester=$_POST['course_semester'];
        $con=($GLOBALS["___mysqli_ston"] = mysqli_connect($server,  $username,   $password));
        if(!$con)
        {
        exit('Error: could not establish connection to the server');
        }
        else
        {
        $con_db=((bool)mysqli_query($con, "USE esther"));
        if(!$con_db)
        {
        exit('Error: Failed to connect to the database');
        }
        else
        {
        if(!empty($course_code) && !empty($course_title) && !empty($course_units) && !empty($course_semester))
    {
        $insert="INSERT INTO `course_table` VALUES('', '".$course_code."' ,'".$course_title."','".$course_units."','".$course_semester."')";
        $query=mysqli_query($GLOBALS["___mysqli_ston"], $insert);   
        $dup_admission_number = mysql_query("SELECT admission_number FROM users_table WHERE admission_number = $admission_number");
    }
    if (@mysql_query($dup_admission_number)) {
        echo 'Your admission number is already in our database.';
    exit;
    }
        if($query)
        {
                echo 'course added successfully!';
                header("location:add_course.php");      
        }
        else { echo 'Error while adding Course.'; }
    }
    else
    {
        echo '*** fields cannot be blank ***.';
    }
}
}
?>
 
     
    