预订系统。 检查现有数据

I want to check and compare the information on my input form with information that is stored in my database. Basically. if trainer, sessionslot eventdate is the same dbtrainer, dbeventdate dbsessionslot ECHO "Booked"; Else insert into booking table

I know very little about programming, could really do with some help on this one.

This is snippet of the code i am using.

if(isset($_GET['add'])){
    $trainee = $_POST['txttrainer'];
    $trainer = $_POST['txttrainee'];
    $sessionSlot = $_POST['txtsession'];


    $eventdate = $month."/".$day."/".$year;
    $query = mysql_query("SELECT * FROM BOOKING WHERE trainer='$trainer' AND SessionSlot='$sessionslot");


    $sqlinsert = "insert into booking (Trainee,Trainer,sessionSlot,eventDate,dateAdded) values ('".$trainee."','".$trainer."','".$sessionSlot."','".$eventdate."',now())";
    $resultinsert = mysql_query($sqlinsert);

            $numrows = mysql_num_rows($query);
            if($numrows == 1) {
                echo "this timeslot is booked"
                if($resultinsert){
                    echo "Booking Successful....";
                 }else{
                     echo "Booking Failed";
                 }
             }
}

If you call mysql_query, the query is executed. So you're executing the INSERT before you check whether the SELECT returned a row or not.

That means you should place the INSERT part inside an if($numrows != 1) condition.

Please be aware that using mysql_query is deprecated: http://ch1.php.net/manual/en/function.mysql-query.php and you should use MySQLi or PDO_MySQL. Your code is vulnerable to SQL injections.

    if(isset($_GET['add'])){
    $trainee = $_POST['txttrainer'];
    $trainer = $_POST['txttrainee'];
    $sessionSlot = $_POST['txtsession'];


    $eventdate = $month."/".$day."/".$year;
    $query = mysql_query("SELECT * FROM BOOKING WHERE trainer='$trainer' AND SessionSlot='$sessionslot");


    $sqlinsert = "insert into booking (Trainee,Trainer,sessionSlot,eventDate,dateAdded) values ('".$trainee."','".$trainer."','".$sessionSlot."','".$eventdate."',now())";


            $numrows = mysql_num_rows($query);
            if($numrows >0) {
                echo "this timeslot is booked"
             }else{
                 $resultinsert = mysql_query($sqlinsert);
                 if(mysql_error()==""){
                    echo 'time slot booked';
                 }else{
                    echo 'error';
                 }
             }
    }

Explanation:

if there are rows selected, the timeslot is booked, else execute the query. If there is no error with the query, then print out success.