<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/head-lib.php');

echo "<pre>";print_r($_POST);echo "</pre>";

// CHECK

	if( strlen($_POST['intitule']) < 3 || strlen($_POST['lieu_delivrance']) < 3 || strlen($_POST['etablissement']) < 3 || strlen($_POST['annee_obtention']) < 4){
		Header("Location: ".$_SERVER['HTTP_REFERER']."?alert=".urlencode('Il y a une erreur. Veuillez réessayer.')."#".$_GET['tab']);
		exit();
	}

// DETECTION NOUVEAU DIPLOME OU NON

	else{
		if($_POST['id_diplome'] == "new"){
			dbquery("INSERT INTO cv_diplomes(id_cv) VALUES('".$_SESSION['user']['id_cv']."')") ;
			$sql			= dbquery("SELECT * FROM cv_diplomes WHERE id_cv = '".$_SESSION['user']['id_cv']."' ORDER BY id_diplome_cv DESC LIMIT 1");
			$id_diplome		= dbresult($sql,0,"id_diplome_cv");
		}
		else{
			$id_diplome = $_POST['id_diplome'] ;
		}

// ID NIVEAU DIPLOME

	$code_diplome		= $_POST['diplome_intitule_'.$_POST['diplome_type']];
	$sql				= dbquery("SELECT * FROM codes_diplomes WHERE id_code_diplome = '".$code_diplome."'");
	$id_niveau_diplome	= dbresult($sql,0,"id_niveau_diplome");
	

// DB UPDATE
	
		dbquery("UPDATE cv_diplomes 

					SET id_cv				= '".$_SESSION['user']['id_cv']."', 
						type				= '".$_POST['diplome_type']."',
						code_diplome		= '".$code_diplome."',
						id_niveau_diplome	= '".$id_niveau_diplome."',
						specialite			= '".$_POST['specialite']."',
						intitule			= '".addslashes($_POST['intitule'])."',
						etablissement		= '".addslashes($_POST['etablissement'])."',
						lieu_delivrance		= '".addslashes($_POST['lieu_delivrance'])."',
						annee_obtention		= '".addslashes($_POST['annee_obtention'])."'

					WHERE id_diplome_cv = ".$id_diplome."
		");

		Header("Location: ".$_SERVER['HTTP_REFERER']."#".$_GET['tab']);
		exit();	

	}
?>