<?php
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');

if(isset($_SESSION['user_droits'])) {
	$carac = array('[','"',']');
	$replace = array('','','');

	$savoirsapd = str_replace($carac,$replace,$_REQUEST['savs']);
	$savoirs = explode(',',$savoirsapd);
	// suppression des liens existants :
	mysqli_query($bdd,"DELETE FROM ".$prefix."_seqsav WHERE seq_id = '".$_REQUEST['seq_id']."' AND s_id LIKE '".$_REQUEST['item']."%' OR s_id = '-1'");
	// création des liens entre séquence et savoirs :
	foreach($savoirs as &$sav) {
		if($sav!=""&&$sav!="-1") {
			// vérification qu'un lien n'existe pas déjà
			$seq_verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqsav WHERE seq_id = '".$_REQUEST['seq_id']."' AND s_id = '".$sav."'");
			$seqnb_verif = mysqli_num_rows($seq_verif);
			if($seqnb_verif==0) {
				// insertion le cas échéant
				mysqli_query($bdd,"INSERT INTO ".$prefix."_seqsav (seq_id, s_id) VALUES ('".$_REQUEST['seq_id']."', '".$sav."')");
			}	
		}
	}
}
mysqli_close($bdd);
?>