0

Problem

So, my problem is that my program allows the admin to input a teacher's first and last name, and than checks if the teacher already exists in the db. If the teacher already exists in the db, the program should run: header("location:../../admin.php?msg=Teacher already exists");, but if the teacher doesn't exist in the db, the program should put the teacher in the db. But, when I tested the program, even though the teacher didn't exist in the db, the program ran: header("location:../../admin.php?msg=Teacher already exists");, instead of inserting the teacher into the db.

PHP Code

<?php 

require '../connect.php';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

// check if inpusta are not empty
if(!empty($firstname) && !empty($lastname)) {

    // check if teacher isn't already in the database
    $getTeacher = $link->prepare("SELECT * FROM teachers
                                WHERE firstname = :firstname
                                AND lastname = :lastname");
    $getTeacher->execute(array(
        "firstname" => $firstname,
        "lastname" => $lastname,
    ));
    $getTeacher = $getTeacher->fetch();

    // if teacher doesn't exist in db
    if(!getTeacher) {

        // insert teacher into db
        echo "Success";

    } else {
        header("location:../../admin.php?msg=Teacher already exists");
    }

} else {
    header("location:../../admin.php?msg=Required inputs must be filled");
}

?>

1 Answers1

0

You miss-typed the variable $getTeacher.
if(!getTeacher) { should be if(!$getTeacher) {

gaganshera
  • 2,629
  • 1
  • 14
  • 21