<?php
include("../../includes/connect.php");
print_r($_POST);
echo "<hr />";

     echo "Modifico il set di domande";
     $questionSetId = $_POST['questionSetId'];
    $questionSetName = $_POST['commentSetName'];
    $questionSetLanguage = $_POST['questionSetLanguage'];
    
    $connection->query("UPDATE comments_sets SET set_name = '$questionSetName',set_language='$questionSetLanguage' WHERE id = '$questionSetId'");

    // elimino le domande già esistenti 
    $domande = $_POST['domande'];

 $connection->query("DELETE FROM comments WHERE sets_in = '$questionSetId'");
// ricreo i commenti

for($d = 0; $d < count($domande); $d++)
{
    $commentText = $domande[$d]['domanda'];
    //$connection->query("INSERT INTO comments (text,isChildren,parentComment,sets_in) VALUES ('$commentText',0,0,'$questionset')");

    $qdomanda = $connection->prepare("INSERT INTO comments (text,isChildren,parentComment,sets_in) VALUES (?,?,?,?)");
    $qdomanda->execute(["$commentText",0,0,"$questionSetId"]);

    

    $idComment = $connection->lastInsertId();
    for($r = 0; $r < count($domande[$d]['risposte']); $r++)
    {
        $subCommentText = $domande[$d]['risposte'][$r]['risposta'];
        $qrisposta = $connection->prepare("INSERT INTO comments (text,isChildren,parentComment,sets_in) VALUES (?,?,?,?)");
        $qrisposta->execute(["$subCommentText",1,$idComment,"$questionSetId"]);

    
    }
}

?>