<?php
	session_start();
	
	include('../config_inc.php'); 
	
	if($_SESSION['database']=="local")
	  {
	     $db_host    = "localhost";
	     $db_user   = "root";
	     $db_pass   = "";
	     $db_name   = "boutique";	
	 }
	
	if($_SESSION['database']=="public")
	  {
  	     $db_host    = "sql2";
   	     $db_user   = "analogde";
 	     $db_pass   = "rainbow";
	     $db_name   = "analogde";
	     //$db_type   = "mysql";	
	 }

	if(isset($_POST['valider']) or isset($_POST['ecraser']))
	  { 
	  
	     for($i=0;$i<count($_SESSION['panier']['reference']);$i++)
	        { 
	   	$reference = $_SESSION['panier']['reference'][$i];
	   	$designation = $_SESSION['panier']['designation'][$i];
		$table = $_SESSION['panier']['table'][$i];
	     	$qte = $_SESSION['panier']['qte'][$i];
	     	$prix_ttc = $_SESSION['panier']['prix_ttc'][$i];
	             
	        $calcul = $_SESSION['panier']['prix_ttc'][$i]*$qte;     
	             
	        $total_ttc = $total_ttc + $calcul;
	            
	             if($i==0)
	             { $chaine_panier = $reference ."- " .$table ."- " .$designation  ."- " .$qte ."- " .$prix_ttc ;
	             }	
	             else {
		              $chaine_panier = $chaine_panier ."- " .$reference ."- " .$table ."- " .$designation  ."- " .$qte .", " .$prix_ttc ;	
			  }
		}       
		
		
		if(isset($_POST['titre']))
	          {
	  		$titre = $_POST['titre'];
	  	
	  		$email = $_SESSION['email'];	
			//$email = "patrice.delpy@onsemi.com";
	  	
	  		// vérifier si le titre existe déjà
	  	
	  		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
                	mysql_select_db($db_name, $connexion);
	  	
	  		$requete = " SELECT * FROM panier WHERE titre = '$titre' and email = '$email' ";
	  		//echo $requete;
	    		$result = mysql_query($requete);
	    
	    		$nb_resultats_filtre = mysql_num_rows($result);
    	
	    		if($nb_resultats_filtre != 0)
	          	{ 	// Existe
	          		header('Location: information.php');
	  	  	}
	  	
	  	   else {	  	
			   $connexion = mysql_connect($db_host,$db_user,$db_pass); 
                	   mysql_select_db($db_name, $connexion);
	
			   $date = date("d-m-Y"); 
			   $heure = date("H:i");
		
			   $requete = " INSERT INTO `panier` (`id`, `email`, `panier`, `heure`, `date`, `titre`, `total_ttc`) VALUES
			   ('', '$email', '$chaine_panier', '$heure', '$date', '$titre', '$total_ttc') ";

			   //echo $requete;

			   $result = mysql_query($requete);
		
			   header('Location: panier02.php');
		       }	

	         }	      
	  

	  }
	  
	if(isset($_POST['abandonner']))
	  {  header('Location: rafraichir_panier02.php');
	  }  

?>


	<TABLE width=100% border=0>
	<TR valign=top>
	<TD width=50%>
	<FONT face="Arial" size=2 class="size2">
		
		Le titre de votre panier existe dèjà. Vous devez définir un autre titre sinon ce panier sera écrasé.
		
	</FONT>
	</TD>
	</TR>
	</TABLE>

	
	<FORM action="information.php" method="post">

	<CENTER>
	<TABLE border=0 cellspacing=1 cellpadding=4>
	<TR valign=top>
	<TD><FONT class=size2>Titre de votre sauvegarde:</FONT></TD>
	<TD><INPUT type=text name=titre value="" size=40 maxlength=40></TD>
	</TR>
	</TABLE>
	
	<INPUT type="submit" name="valider" value="Valider">
	<INPUT type="submit" name="ecraser" value="Ecraser">
	<INPUT type="submit" name="abandonner" value="Abandonner">
	
	</center>
        <br>
        <center>
        <table width=95% border=0 cellspacing=1 cellpadding=4>
        </center>
        </form>
	
	
	