[英]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.任何帮助将非常感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.