You can retrieve the most recent automatically generated AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function. These functions are connection-specific, so their return values are not affected by another connection which is also performing inserts.
https://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html
Use this query in the field when you insert. Sounds like your having problems and testing a lot of code. You should test queries out before updating tables.