<?php
include("connection.php");
include("frames_header.php");
?>

<?php
    $dev=$_POST["dev"];
    $fatelist=$_POST["fatelist"];
	$fatelist=split(":",$fatelist);
    $allfates = $_POST["f"];

	$tab=array();
	for($a=0;$a<count($allfates);++$a){
		list($fate,$id) = split ("--", $allfates[$a]);
		$tab[$id][$fate]='y';
	}

pg_query($c,"begin");
	for($yy=0; $yy<count($fatelist); ++$yy ){
		$word=$fatelist[$yy];

		$requete= "SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"DEV_STAGE_ID\"='$dev'";
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		while($row=pg_fetch_array($stmt)){
			$key=$row["ANAT_ID"];
			if($tab[$key][$word]=='y'){
				//verif si l'entree existe deja
				$requete= "SELECT \"KEYWORDS\" from \"FATE\" where \"ANAT_ID\"=$key and \"KEYWORDS\"='$word'";
				$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				if(!$row2=pg_fetch_array($stmt2)){
					$requete="insert into \"FATE\"(\"ANAT_ID\", \"KEYWORDS\" )values($key, '$word')";
					
					$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					
				}
			}else{
				$requete= "DELETE from \"FATE\" where \"ANAT_ID\"=$key and \"KEYWORDS\"='$word'";

				$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				

			}
			
		}
	}
	
	// Validation de la transaction
pg_query($c,"commit");

//	$committed = OCICommit($connection);
		
	// Verification de la validation : si une erreur est survenue, afficher le message d'erreur
	    print "<br><center><H1>Update done</H1></center>";

	
	print '<center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';

?>

<?php
    include("frames_footer.php");
?>