I have a problem deleting all child nodes and parent nodes using with PHP recursive function. The database table structure is followed the tree structure.
Below is sample table structure:
MySQL Table: folders
id name parentid
1 A1 0
2 A1-1 1
3 A1-2 2
4 A1-3 3
5 A2 0
6 A2-1 5
7 A2-2 6
8 A2-3 7
id = the id of the category
name= folder name
parent = the id of the parent category
According to the table: folders. For example, if I want to delete id is 1, following the child nodes (id are 2,3,4) should be deleted.
I am followed the below sample coding to do the delete recursive function, but can't work and crashes.
<?php
function remrecurs($id) {
$qlist= mysqli_query($sql_connection,"SELECT * FROM folders WHERE parentid='$id'");
if (mysqli_num_rows($qlist)>0) {
while($curitem = mysqli_fetch_assoc($qlist)) {
remrecurs($curitem['id']);
}
} mysqli_query($sql_connection,"DELETE FROM folders WHERE id='$id'");
}
remrecurs(1);
?>