简体   繁体   English

试图获取非对象的属性 - PHP

[英]Trying to get property of non-object - PHP

So I'm trying to get the teacher_id that is corresponding to the teacher's first and last name that the user has entered, but when I try to get the teacher_id it outputs Trying to get property of non-object . 所以我试图获得与用户输入的老师名字和姓氏相对应的teacher_id ,但是当我尝试获取teacher_id它输出Trying to get property of non-object Does anyone have any ideas? 有没有人有任何想法?

PHP PHP

<?php

// PROCESSES STUDENT INFO

// get connect page
require '../../connect.php';

// get input info
$student_id = $_POST['student_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$teacher_first_name = $_POST['teacher_first_name'];
$teacher_last_name = $_POST['teacher_last_name'];

// check if input is not empy
if(!empty($student_id) && !empty($first_name) && !empty($last_name) && !empty($teacher_first_name) && !empty($teacher_last_name)) {

    // check if numeric inputs have a number
    if(is_numeric($student_id)) {
        $teacher_check = mysqli_query($link, "SELECT teacher_id FROM teachers WHERE first_name='$teacher_first_name' AND last_name='$teacher_last_name'");

        // check if teacher exists
        if($teacher_check) {
            $row = $teacher_check->fetch_object();

            $result = mysqli_query($link, "INSERT INTO students (student_id, first_name, last_name, teacher_id) VALUES ($student_id, '$first_name','$last_name', $row->teacher_id)");

            if($result) {
                header("Location: ../../../admin.php?message=Success!");
            } else {
                echo mysqli_error($link);
                // header("Location: ../../../admin.php?message=Sorry we ran into an error");
            }
        } else {
            header("Location: ../../../admin.php?message=Teacher Does Not Exist");
        }
    } else {
        header("Location: ../../../admin.php?message=Please add a number for Student ID");
    }

} else if (empty($student_id) || empty($first_name) || empty($last_name)) {
    header("Location: ../../../admin.php?message=Please add you're input values");
}

?> ?>

change this line, you are checking whether the query is ok but you have not checked whether it has any results. 更改此行,您正在检查查询是否正常,但您尚未检查它是否有任何结果。

if($teacher_check) {
        $row = $teacher_check->fetch_object();

to (this line checks whether you have any result data if you have result data you have it $row otherwise null) to(如果您有结果数据,此行检查您是否有任何结果数据$ row否则为null)

if($row = $teacher_check->fetch_object()){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM