WP Database Operations
You may need to follow database-related code when you develop plugins for WordPress
How to get the last inserted id of the table?
You can use the following code straight after the
$lastid = $wpdb->insert_id;
Make sure you have the primary key, auto-increment, unique for the id column