<?php 

	include("connexion_db.php");

	$resultat = '';

	// Verification de la persistance des données
	if ($_SERVER['REQUEST_METHOD'] === 'POST') {

		$idQues = $_POST['idQuestion'];
		$idPatient = $_POST['id_patient'];
		$oneReponseFonction = $_POST['reponse'];

	

			$sqlveri = "SELECT date FROM reponse_all r where r.id_question='".$idQues."' AND r.id_patient='".$idPatient."' ORDER by r.date ASC";
			$reqq = cleanPDOQuery($sqlveri);
			$dateSQQQLLL='';
			while($res = $reqq->fetch()){
				$dateSQQQLLL = $res['date'];
			}


			$now = new DateTime();
			$now = $now->format('Y-m-d');
			$resultat = $now;
			if($dateSQQQLLL === $now){
				
					$sqla = "UPDATE reponse_all SET reponse= '".$oneReponseFonction."' WHERE id_question='".$idQues."' and id_patient='".$idPatient."' and date = '".$now."'";
					$reqa = cleanPDOQuery($sqla);
					$resultat = $sqla;
				


			}else{	
					$sql = "INSERT INTO reponse_all(id_patient,id_question,reponse,date) VALUES('".$idPatient."','".$idQues."','".$oneReponseFonction."',now())";
					$req = cleanPDOQuery($sql);
					$resultat = $sql;
		}		

	}


	header('Content-Type: application/json');
 	echo json_encode($resultat);
		

 ?>