I have a method in my Event class that adds an Event to the database. I want to return the id of the inserted event so that I can have my event form retain state after the admin is redirected back to the form with a ‘success’ message.
This is what I have tried:
/*
* Add a new event
*/
public function addEvent($data) {
$this->db->query("INSERT INTO events (active, event_name, event_location, event_descrip, event_date, event_time, instructions, end_date)
VALUES (:active, :event_name, :event_location, :event_descrip, :event_date, :event_time, :instructions, :end_date)");
$this->db->bind(':active', $data['active']);
$this->db->bind(':event_name', $data['event_name']);
$this->db->bind(':event_location', $data['event_location']);
$this->db->bind(':event_descrip', $data['event_descrip']);
$this->db->bind(':event_date', $data['event_date']);
$this->db->bind(':event_time', $data['event_time']);
$this->db->bind(':instructions', $data['instructions']);
$this->db->bind(':end_date', $data['end_date']);
if ($this->db->execute()) {
$id = $this->db->lastInsertId();
return $id;
} else {
return false;
}
}
I am getting the following error: “Fatal error: Call to undefined method Database::lastInsertId() in C:\xampp\htdocs\registration2\libraries\event.class.php on line 107”
At the beginning of the Event class I have
private $db;
/*
* Constructor
*/
public function __construct() {
$this->db = new Database;
}
What have I done wrong here?