04-09-2017, 04:17 PM
Hey,
I'm new to PHP and programming in general.
There is something I noticed while working with the UserSpice profile feature.
users/profile.php is working properly, if $userQ gets an existing user id. (line 34)
if a user id, in my case profile.php?id=4, does not exist in the table, php throws the following:
Notice: Undefined offset: 0 in C:\xampp\htdocs\userspice\users\classes\DB.php on line 155
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 46
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 47
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 49
So, we're declaring variables in if(isset($_GET['id'])){ , but obviously missing a check, if user id is an existing db entry. The else (line 53) will excecute, when there is nothing "to get". If there is, we have no fallback.
My thought was to make another query for checking if the $userID is in the table (findById?). But that doesn't work as expected..
So, after $thatUser = $userQ->first(); on line 35, i tried
if(empty($thatUser)){
die('No entry');
} else{
But still have the problem, that $userQ->first(); does not exist and causes the offset:0 error.
I think there is a fast and smoother way to accomplish this but i don't get it. Like:
if id isset
// check the id
if id exists
//if yes, return
// if not, do something else (some static html 'User does not exist')
Would be awesome if someone can help.
I'm new to PHP and programming in general.
There is something I noticed while working with the UserSpice profile feature.
users/profile.php is working properly, if $userQ gets an existing user id. (line 34)
if a user id, in my case profile.php?id=4, does not exist in the table, php throws the following:
Notice: Undefined offset: 0 in C:\xampp\htdocs\userspice\users\classes\DB.php on line 155
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 46
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 47
Notice: Trying to get property of non-object in C:\xampp\htdocs\userspice\users\profile.php on line 49
So, we're declaring variables in if(isset($_GET['id'])){ , but obviously missing a check, if user id is an existing db entry. The else (line 53) will excecute, when there is nothing "to get". If there is, we have no fallback.
My thought was to make another query for checking if the $userID is in the table (findById?). But that doesn't work as expected..
So, after $thatUser = $userQ->first(); on line 35, i tried
if(empty($thatUser)){
die('No entry');
} else{
But still have the problem, that $userQ->first(); does not exist and causes the offset:0 error.
I think there is a fast and smoother way to accomplish this but i don't get it. Like:
if id isset
// check the id
if id exists
//if yes, return
// if not, do something else (some static html 'User does not exist')
Would be awesome if someone can help.