简体   繁体   English

为什么我在 PHP 中收到“试图获取非对象的属性”错误?

[英]Why am I getting a "trying to get a property of a non-object" error in PHP?

I'm working on a form where you can update a student's information in the database.我正在处理一个表单,您可以在其中更新数据库中学生的信息。

I've been getting this error:我一直收到这个错误:

Notice: Trying to get property of non-object in [...]/EditPersonalInfo.php on line 54.注意:尝试在第 54 行的 [...]/EditPersonalInfo.php 中获取非对象的属性。

I know this is a common error asked about on here but none of the solutions I found so far have fixed the issue in my case.我知道这是这里询问的常见错误,但到目前为止我找到的解决方案都没有解决我的问题。

Here is my code:这是我的代码:

// At the top of the page I also made sure to require_once my server.php file, so the database does connect

<?php 
        if (isset($_POST['update_personal'])) {

            $phone = trim($_POST['phoneNumber']);       
            $address = trim($_POST['address']); 
            $id = $_SESSION['id'];

            // The three variables above work when echoed.

            $query = "UPDATE StudentInfo SET PhoneNumber = $phone , Address = '$address' WHERE Student_ID = $id";

            // The above looks fine when echoed

            // The problem starts here
            $result = $conn->query($query);

            // Line 54
            if($result->num_rows == 1) {        

               // This was an attempt to set session variables when things didn't work. Nothing
               // in the database has changed though.
               while ($row = $result->fetch_assoc()){
                    $_SESSION['address'] = $row['Address'];
                    $_SESSION['phone'] = $row['PhoneNumber'];
                    }
            }

        }
    ?>

This same approached worked earlier when I was setting session variables:当我设置会话变量时,同样的方法也有效:

// When user submits login info.
    if (isset($_POST['login_user']))
    {
        // Get info from forms and trim white space, then save as variables.
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);

        // If no errors.
        if (count($errors) == 0) {

            // Select user info from server. 
            $query = "SELECT * FROM StudentInfo WHERE Username='$username' AND Password='$password'";

            $result = $conn->query($query);

            if($result->num_rows == 1) 
            {
            // Load user from server, send to home page

                while ($row = $result->fetch_assoc()){
                    $_SESSION['id'] = $row['Student_ID'];
                    $_SESSION['fname'] = $row['FirstName'];
                    $_SESSION['lname'] = $row['LastName'];
                    $_SESSION['dob'] = $row['DateOfBirth'];
                    $_SESSION['address'] = $row['Address'];
                    $_SESSION['email'] = $row['EmailAddress'];
                    $_SESSION['due'] = $row['AmountDue'];
                    $_SESSION['phone'] = $row['PhoneNumber'];
                    $_SESSION['emergency'] = $row['EmergencyContact_ID'];
                    $_SESSION['coursesid'] = $row['CourseList_ID'];
                    $_SESSION['res'] = $row['Residency_ID'];
                    $_SESSION['user'] = $row['Username'];
                    $_SESSION['pass'] = $row['Password'];

                }

            header('location: home/home.php');
            }
        }
    }

Any help would be much appreciated.任何帮助将非常感激。

the problem is with $result->num_rows use rowCount问题在于$result->num_rows使用rowCount

so correct code will be所以正确的代码将是

    if($result->rowCount() == 1) 

暂无
暂无

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

相关问题 为什么会出现下一个错误:试图获取非对象的属性? laravel - Why am I getting next error: Trying to get property of non-object? laravel 为什么我要尝试获取AngularJs中非对象错误的属性? - Why i am getting trying to get property of non-object error in AngularJs? 为什么我收到通知:试图获取非对象错误的属性? - Why am I getting Notice: Trying to get property of non-object error? 为什么我要在Laravel Eloquent中尝试获取非对象的属性 - Why I am getting Trying to get property of non-object in Laravel Eloquent 我从控制器收到“尝试获取非对象的属性”错误 - I am getting a “Trying to get property of non-object” error from my controller 我收到此错误“试图获取非对象的属性”,但它在另一台计算机上工作正常吗? - I am getting this error "Trying to get property of non-object" , but its working fine in another Computer? 我正在尝试从数据库中获取价格字段值 .getting 错误 #message:“试图获取非对象的属性” - I am trying to get price field values from database .getting error #message: “Trying to get property of non-object” 为什么在尝试访问“$comment-&gt;users-&gt;username”时出现“试图获取非对象的属性‘用户名’”? - Why am I getting "Trying to get property 'username' of non-object" when trying to access "$comment->users->username"? PHP获取API尝试获取非对象的属性 - PHP getting an API Trying to get property of non-object 获取消息错误PHP:尝试在Codeigniter中打印数据时尝试获取非对象的属性 - Getting message error PHP: Trying to get property of non-object when trying to print my data in Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM