Remove usage of last_insert_id

`last_insert_id()` is extremely buggy and can often return `0`. Instead, get the `id` using a select statement.