Mysql – Design problem with auto-increment fields

auto-incrementdatabase-designMySQL

I've made this design similar to my problem, it's not exact but it close enough to understand my problem:

enter image description here

Some cellular client frequently send their GPS position and the WLAN name that they have in range, sometimes only their position.

I've auto-generated timestamp and auto-incremental idModelGPS from ModelGPS table and I'm using javaDB to write to the DB.

I want to link the ModelGPS to an WLAN name where it happens, but I can't do it because the idModelGPS is autogenerated. I cannot access it to write in the ModelGPS_WLAN table.

I don't know how to handle this, maybe there is a design error or maybe there is a way to solve this problem that I'm unaware of. I'll be glad to hear your opinions.

Best Answer

So you just need to fetch the last auto-increment value that was inserted? There are a couple of ways to do this, but they're all pretty simple.

Query: SELECT LAST_INSERT_ID()

php mysql: $id = mysql_insert_id($mysql_conn);
http://php.net/manual/en/function.mysql-insert-id.php

php mysqli: $id = $mysqli->insert_id;
http://php.net/manual/en/mysqli.insert-id.php

Both of those links have some example code that looks pretty easy to follow.