# \[PHP, MySQL\] - Updating composite tables

**URL:** https://forum.kirupa.com/t/php-mysql-updating-composite-tables/195335
**Category:** programming
**Created:** [July 31, 2006, 12:52am UTC](https://forum.kirupa.com/t/php-mysql-updating-composite-tables/195335 "2006-07-31T00:52:08Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![redrum87](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@redrum87](https://forum.kirupa.com/u/redrum87)
#### Post date: [July 31, 2006, 12:52am UTC](https://forum.kirupa.com/t/php-mysql-updating-composite-tables/195335/1 "2006-07-31T00:52:08Z")

</div>

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:
