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.
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.