This is an old chestnut but I cannot see the error in my code. I'm in Windows 10, VSCode and using the Codeigniter 3 framework. I am trying to delete a record but also add an alert saying that the record has been successfully deleted. So first I use the id and get the record, save it as session data, delete the record and then put up the alert using the saved data like this 'You have successfully deleted the database entry for John Smith'. Here is my controller method:
function member_delete($id) {
$member = $this->get_where($id);
$this->session->set_userdata('firstname',$member->firstname);
$this->session->set_userdata('lastname',$member->lastname);
if($this->_delete($id)){
$firstname = $this->session->userdata('firstname');
$lastname = $this->session->userdata('lastname');
$this->session->set_flashdata('success', 'You have successfully deleted the database entry for '.$firstname.' '.$lastname);
$this->members();
} else {
$this->session->set_flashdata('failure','There has been an error and the record has not been deleted');
$this->members();
}
}
This works fine and the alert is successfully posted but then when I then refresh the page PHP throws an (Severity:notice) error 'Trying to get the property of a non-object'. The error line is the second line of the method ie $method->firstname.
I have run var_dump on the variable $member and it is definitely an object, being returned with result->row().
Can anyone tell me what is going wrong?
You're fetching $member - but then you try to access properties without knowing $member was successfully filled in the previous line
So I'd start with :
$member = $this->get_where($id);
if ( is_a($member, "someClass") )
{
$this->session->set_userdata('firstname',$member->firstname);
$this->session->set_userdata('lastname',$member->lastname);
} else
// process failure
You said the var_dump() returns an object - but does it return the object you're expecting ? Do all expected properties exist ?
Secondly, who or what is calling this function ? If you do a refresh of the page, obviously member_delete() is called with a value of $id which shouldn't be there anymore (as you deleted it the last cycle). So either $id is persistent for some reason, or something is invoking it with obsolete data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.