We will use the exec
method to add a new record. Then, if we want to get the ID of the line we added, we will call the lastInsertId
method. If there is a problem while adding, the exec method will return false. For this reason, we can first check whether it is added or not, and then print the ID on the screen
<?php
if($db->exec('INSERT INTO users (name) VALUES ("Baransel")'))
{
$id = $db->lastInsertId();
echo 'ID of the newly added member: ' . $id;
}
else
{
echo 'An error occurred while adding a new record.';
}
?>