/**
 * Gibt alle Childs in einem Array zurück.
 * @param int $id
 * @return array
 */
function GetChilds($id)
{
    $tasks = array();
    require 'config.php';
    $sql = "SELECT * FROM tasks WHERE parent = " . $id;
    $result = mysqli_query($con, $sql);
    while ($row = mysqli_fetch_array($result)) {
        $tasks[] = $row['taskid'];
        if (TaskHasChilds($row['taskid'])) {
            $tasks[] = GetChilds($row['taskid']);
        }
    }
    return $tasks;
}
<?php

require_once 'template/header.php';
require_once 'template/navigation.php';
if (empty($_GET['id'])) {
    if (!empty($_SERVER['HTTP_REFERER'])) {
        header("Location: " . $_SERVER['HTTP_REFERER']);
    } else {
        header("Location: index.php");
    }
}
if (isset($_POST['delete'])) {
    $tasks = GetChilds($_GET['id']);
    $flattentasks = array();
    flattenarray($tasks, $flattentasks);
    foreach ($flattentasks as $index) {
        $sql = "DELETE FROM `tasks` WHERE `taskid` = " . $index;
        mysqli_query($con, $sql);
    }
    $comments = "DELETE FROM `comments` WHERE `taskid` = " . $_GET['id'];
    mysqli_query($con, $comments);
    $history = "DELETE FROM `statushistory` WHERE `taskid` = " . $_GET['id'];
    mysqli_query($con, $history);
    $sql = "DELETE FROM `tasks` WHERE `taskid` = " . $_GET['id'];
    mysqli_query($con, $sql);
    $sql = "DELETE FROM `recurring_tasks` WHERE `taskid` = " . $_GET['id'];
    mysqli_query($con, $sql);
}
if (!TaskExists($_GET['id'])) {
    header("Location: tasks.php");
    die;