i could not figure it out how to delete subs sub categories when i delete main category.
I made two queries. This works when i delete sub and sub sub. But if the category i am deleting is the main cat. i can not delete the subs sub.
What should i do here to delete the car, fiat and panda if the id is 1?
cats_id cats_parentid cats_name
1 0 car
2 1 fiat
3 2 panda
mysql_query("DELETE FROM cats
WHERE cats_id='".$id."'") or die(mysql_error());
mysql_query("DELETE FROM cats
WHERE cats_parentid='".$id."'") or die(mysql_error());
You can use recursive function calls like this (in PHP):
Or if you use InnoDB engine for your tables you need to create foreign key
and after you delete parent record all the connected records will be deleted by MySQL database without other queries.