Jerry, you're not a loyal reader of my blog, are you?

You shouldn't have db_dml statements inside a db_foreach or vice versa. Here's how to get around it:
http://rubick.com:8002/blogger/one-entry?entry%5fid=1643
Of course, the problem I have with that sometimes is that db_list and db_list_of_lists do not guarantee the order of the lists. So sorting gets screwed up.
So then you have to sort it. Ug.