Onloac Posted October 6, 2009 Share Posted October 6, 2009 hello, I have a script that helps me manage my site. I've coded it from the ground up and everything works fine. Now I'm just looking to tweak things so it'll work to my advantage more. This is what I'm triyng to do. I have an id that is used in one table (for my categories)... and this ID is used in another table (for my articles). Now when I edit a category ID how would I go about updating the ID on the other table? I would search google but I'm not sure what to look for? Does anyone know any good tutorials or terms I can search for to accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/176665-updating-something-in-php-and-mysql/ Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Well firstly I really don't see a reason why you should need to update an ID. The whole point of an ID is that it uniquely identifies a row. If for some reason this was a requirement (I can't think of a valid one, but maybe you can prove me wrong), I guess you would have to update links to the ID before updating the ID using an UPDATE table SET cat_id=$new_id WHERE cat_id=$old_id. Quote Link to comment https://forums.phpfreaks.com/topic/176665-updating-something-in-php-and-mysql/#findComment-931389 Share on other sites More sharing options...
DavidAM Posted October 6, 2009 Share Posted October 6, 2009 If you are using the InnoDB Storage Engine, you should define the ID in the articles table as a Foreign Key to the Catagories table. Part of the FKey definition is how to handle DELETES or UPDATES of the key. ON UPDATE CASCADE should cause the ID to be updated in the "foreign" table (Articles) when you update the ID in the Categories table. Quote Link to comment https://forums.phpfreaks.com/topic/176665-updating-something-in-php-and-mysql/#findComment-931444 Share on other sites More sharing options...
Onloac Posted October 6, 2009 Author Share Posted October 6, 2009 I understand the concept of an ID being uinuqe. All I want to know is how I would go about editing the ID field in one table, after the id is edited in another. From what I can tell "UPDATE table SET cat_id=$new_id WHERE cat_id=$old_id" will change it for me? Seems like it would do what I'm asking, I'll get back and let you know. Thanks for the suggestions and advice! Quote Link to comment https://forums.phpfreaks.com/topic/176665-updating-something-in-php-and-mysql/#findComment-931748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.