[PHP, MySQL] - Updating composite tables

So, lets say I have 3 tables, like so:
[LIST]
[]states(state_id, state**)**
[
]city_lines(state_id, city_id)
[]cities(city_id, city**)**[/LIST]The DB is much larger than this, but this is just an example. I’ve created the insert form for cities in PHP like so:
[LIST=1]
[
]check the cities table for existing cities
[]if city exists, get city_id and skip step 3, if not, continue
[
]insert city into cities table, get city_id
[]insert state_id and city_id into city_lines table[/LIST]And that works great. Now I’m working on the update form, and I’m getting my logic a little bit confused. What needs to happen exactly if the state a city is associated with changes or the city name changes? My thinking is:
[LIST=1]
[
]check the cities table for existing cities
[]if city exists and is not the current city, get city_id and continue.
[
]uhh…?[/LIST]And that’s where I get stuck. I can’t figure out how to prevent duplicate data / change the data thats already there. I just get stuck in this logic trap that I can’t really explain. Help? :hugegrin: