I've done create method which should add new User object into database and on success it should assign id from the auto_increment field in the database.
Everything works except the fact that PDO lastInsertId()
does not output any value.
Here is my code:
public function create() {
$stmt = $this->connect()->prepare(
'INSERT INTO users
(username, password, first_name, last_name)
VALUES (?, ?, ?, ?)');
$stmt->execute(
[$this->username, $this->password, $this->first_name, $this->last_name]);
if ($stmt) {
echo $this->connect()->lastInsertId();
}
}
Method connects()
refers to method in Database class which returns PDO class object.