I want to add a section to a subcategory... the hard part is the database, it really sucks. Unfortunately I have to work with this database for now, can't change anything about it.

- subcat (id, title)
- pages (id, subcat, txt, sections)
subcat foreign key to subcat.id
sections foreign key to section.id
- section (id, title)

pages.sections can contain 0,1 or more section.ids.
pages.subcat either 0 or 1 subcat.id in it.
a subcat can only have 0 or 1 page to it...

This is what I have so far...:

1. add a section to a subcategory. ($subcatid)
1.1 add a new section ($sectiontitle)
1.2 add an exsisting section ($sectionid)

2. new section: INSERT INTO section SET ('','$sectiontitle')
2.1 get new section.id: SELECT id FROM section WHERE title = '$sectiontitle' (by the way: is there a more fool-proof way to do this?) variable => $sectionid

3. check if subcat already has a page.
3.1 if not: INSERT INTO pages SET ('','$subcatid','','')

4. check if pages already contain a sectionid or sectionids
4.1 if not: UPDATE pages SET section = '$sectionid' WHERE subcat = '$subcatid'

4.2 if so:
- SELECT * FROM pages WHERE subcat = '$subcatid'
- $sectionidold = mysql_result($result,0,'sections')
- $sectionidcombined = $sectionidold . ',' . $sectionid (the new one)
- UPDATE pages SET sections = '$sectionsidcombined' WHERE subcat = '$subcatid'

So, what do you think... is this the fool-proof way? Am I making any mistakes? Are there any shortcuts I can take, or maybe there are easier ways to accomplish what I set out to accomplish?

Please advise.