问题
所以,我的问题是我的程序允许管理员输入教师的名字和姓氏,然后检查教师是否已存在于数据库中。如果教师已存在于数据库中,则程序应运行: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");
}
?>
最佳答案
您错过了变量 $getTeacher 的输入。
if(!getTeacher) {
应该是 if(!$getTeacher) {
关于php - 程序输出错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430460/