Because I have so many categories, I created a single “categories” database table :
"id_category , parent_id , category_name , category_tag"
example :
----------------------------------------------------------
| id_category | parent_id | category_name | category_tag |
----------------------------------------------------------
| 1 | 0 | Cars | cars |
| 2 | 1 | Parts | parts |
| 3 | 2 | Accesories | accesories |
----------------------------------------------------------
the tree :
Cars
|_Parts
|_Accesories
My problem occurs when I want to delete all records in the tree.
How do I delete all records in the tree branch from above example.
Is that possible in mySQL with PHP?
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
What you’re looking for is a foreign key on parent_id that references id_category with constraint ON DELETE CASCADE. Then you can just delete the root element (category_id 0) and you should end up with an empty table.