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

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

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

 $connection->query("DELETE FROM profilation_questions WHERE sets_in = '$questionSetId'");
// ricreo le domande 

for($d = 0; $d < count($domande); $d++)
{
    $questionText = $domande[$d]['domanda'];
    $qDomanda = $connection->prepare("INSERT INTO profilation_questions (language,text,sets_in) VALUES (?,?,?)");
    $qDomanda->execute(["$questionSetLanguage","$questionText","$questionSetId"]);

    for($r = 0; $r < count($domande[$d]['risposte']); $r++)
    {
        $answerText = $domande[$d]['risposte'][$r]['risposta'];

        $qRisposta = $connection->prepare("INSERT INTO profilation_answers (question_id,text,sets_in) VALUES (?,?,?)");
        $qRisposta->execute(["$idQuestion","$answerText","$questionSetId"]);
    }
}




?>