程序输出错误信息

时间:2017-04-15 19:37:59

标签: php mysql

问题

所以,我的问题是我的程序允许管理员输入教师的名字和姓氏,而不是检查教师是否已经存在于数据库中。如果教师已存在于数据库中,则程序应运行:header("location:../../admin.php?msg=Teacher already exists");,但如果数据库中不存在教师,则程序应将教师放入数据库中。但是,当我测试程序时,即使数据库中不存在教师,程序也运行:header("location:../../admin.php?msg=Teacher already exists");,而不是将教师插入数据库。

PHP代码

<?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");
}

&GT;

1 个答案:

答案 0 :(得分:0)

你错过了输入变量$ getTeacher if(!getTeacher) {应为if(!$getTeacher) {