I have a table that looks similar to this:
rule_id parent_id
1 0
2 0
3 2
4 2
5 3
When I use a DELETE query and delete let's say id 2, I want to erase the rows that have parent_id as that id.
After deleting successfully, the table should look like:
rule_id parent_id
1 0
5 3
So everything that had 2 is now gone.
I've tried this and I was certain that it would work, but it did not.
$sql = 'DELETE FROM ' . RULES_TABLE .
' WHERE rule_id = ' . (int) $rule_id .
(($type == 'cat') ? ' AND parent_id = ' . (int) $rule_id : '');
You need an 'OR' in your where clause.
DELETE FROM RULES_TABLE WHERE rule_id=? OR parent_id=?
Change your "AND" to "OR":
$sql = 'DELETE FROM ' . RULES_TABLE .
' WHERE rule_id = ' . (int) $rule_id .
(($type == 'cat') ? ' OR parent_id = ' . (int) $rule_id : '');
Might be silly but why don't you just use the old good referential integrity from INNODB?
Setting a reflexive foreign reference key (rule_id, parent_id)
with ON DELETE CASCADE
clause would solve the problem easily.