Pb checkbox retour valeur

Résolu
Bonjour, j'ai une eptite erreur dans mon code que j'arrive pas à déceler :

error_reporting(E_ALL);

 require_once "cnxBDD.php";
 
	$lundi_matin = isset($_POST['lundi_matin']);
	$mardi_matin = isset($_POST['mardi_matin']); 
	$mercredi_matin = isset($_POST['mercredi_matin']); 
	$jeudi_matin = isset($_POST['jeudi_matin']);
	$vendredi_matin = isset($_POST['vendredi_matin']);
	
	
	$lundi_soir = isset($_POST['lundi_soir']);
	$mardi_soir = isset($_POST['mardi_soir']);
	$mercredi_soir = isset($_POST['mercredi_soir']);
	$jeudi_soir = isset($_POST['jeudi_soir']);
	$vendredi_soir = isset($_POST['vendredi_soir']);
		
	if (isset($_POST['lundi_matin'])) {
	$lundi_matin = 1;
	} else {
	$lundi_matin = 0;
	}
	
	if (isset($_POST['lundi_soir'])) {
	$lundi_soir = 1;
	} else {
	$lundi_soir = 0;
	}
	
	if (isset($_POST['mardi_matin'])) {
   $mardi_matin = 1;
	} else {
   $mardi_matin = 0;
	}
	
	if (isset($_POST['mardi_soir'])) {
	$mardi_soir = 1;
	} else {
	$mardi_soir = 0;
	}
	
	if (isset($_POST['mercredi_matin'])) {
   $mercredi_matin = 1;
	} else {
   $mercredi_matin = 0;
	}
	
	if (isset($_POST['mercredi_soir'])) {
	$mercredi_soir = 1;
	} else {
	$mercredi_soir = 0;
	}
	
	if (isset($_POST['jeudi_matin'])) {
   $jeudi_matin = 1;
	} else {
   $jeudi_matin = 0;
	}
	
	if (isset($_POST['jeudi_soir'])) {
	$jeudi_soir = 1;
	} else {
	$jeudi_soir = 0;
	}
	
	if (isset($_POST['vendredi_matin'])) {
   $vendredi_matin = 1;
	} else {
   $vendredi_matin = 0;
	}

	if (isset($_POST['vendredi_soir'])) {
	$vendredi_soir = 1;
	} else {
	$vendredi_soir = 0;
	}
				
	$req = 'INSERT INTO garderie ($lundi_matin, $lundi_soir, $mardi_matin, $mardi_soir, $mercredi_matin, $mercredi_soir, $jeudi_matin, $jeudi_soir, $vendredi_matin, $vendredi_soir)
			VALUES = ""';
	
	$stmt = $bdd->prepare($req);					
	$stmt->bindParam(':lundi_matin',$lundi_matin);
	$stmt->bindParam(':lundi_soir',$lundi_soir);
	$stmt->bindParam(':mardi_matin',$mardi_matin);
	$stmt->bindParam(':mardi_soir',$mardi_soir);
	$stmt->bindParam(':mercredi_matin',$mercredi_matin);
	$stmt->bindParam(':mercredi_soir',$mercredi_soir);
	$stmt->bindParam(':jeudi_matin',$jeudi_matin);
	$stmt->bindParam(':jeudi_soir',$jeudi_soir);
	$stmt->bindParam(':vendredi_matin',$vendredi_matin);
	$stmt->bindParam(':vendredi_soir',$vendredi_soir);
	
	$stmt->execute($req);


J'ai ça en erreur : Warning: PDOStatement::execute() expects parameter 1 to be array, string given in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projet\reservation_garderie.php on line 95

9 réponses

Résumé de la discussion

Problème rencontré lors de l'insertion dans la table garderie : PDOStatement::execute attend un tableau en paramètre, mais une chaîne est fournie, entraînant une erreur et une exécution inhabituelle. Des corrections recommandent d'utiliser une requête INSERT avec des placeholders et de passer un tableau associatif à execute, plutôt que d'envoyer la requête sous forme de chaîne à l'argument. Il faut aussi corriger la syntaxe SQL en utilisant VALUES (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir) et ne pas appeler execute avec une chaîne. En parallèle, il est utile de vérifier le contenu de la variable d'entrée et d'éviter les appels mysql_* obsolètes, car les pratiques anciennes favorisent des divergences entre requête préparée et données transmises.

Bobot (l’IA à votre service)
  1. Modérateur
    Bonjour,

    La récupération des variables ... est à moitié bonne ^^
    Tu as bien utilisé un ISSET.... mais il te manque le "reste" du code.

    Par exemple :
    $lundi_matin = isset($_POST['lundi_matin']);
    

    Ne te retournera que true ou false ... si la variable EXISTE....
    Pour récupérer le CONTENU de ta variable il faut écrire :
    $lundi_matin = isset($_POST['lundi_matin']) ? $_POST['lundi_matin'] : '';
    


    Je te laisse faire la correction pour toutes tes autres variables.

    Ensuite, dans la ligne :
    $stmt->execute($req);
    

    tu ne dois rien mettre entre les parenthèses.....
    (sauf si tu utilises un ARRAY de datas...... mais pas les BINDPARAM )

    1. En modifiant les variables, j'ai rien en enregistrement.
    2. Modérateur
      Montre le code corrigé.
  2. 
     error_reporting(E_ALL);
    
     require_once "cnxBDD.php";
     
    	$lundi_matin = isset($_POST['lundi_matin']) ? $_POST['lundi_matin'] : '';
    	$mardi_matin = isset($_POST['mardi_matin']) ? $_POST['mardi_matin'] : '';
    	$mercredi_matin = isset($_POST['mercredi_matin']) ? $_POST['mercredi_matin'] : '';
    	$jeudi_matin = isset($_POST['jeudi_matin']) ? $_POST['jeudi_matin'] : '';
    	$vendredi_matin = isset($_POST['vendredi_matin']) ? $_POST['vendredi_matin'] : '';
    	
    	$lundi_soir = isset($_POST['lundi_soir']) ? $_POST['lundi_soir'] : '';
    	$mardi_soir = isset($_POST['mardi_soir']) ? $_POST['mardi_soir'] : '';
    	$mercredi_soir = isset($_POST['mercredi_soir']) ? $_POST['mercredi_soir'] : '';
    	$jeudi_soir = isset($_POST['jeudi_soir']) ? $_POST['jeudi_soir'] : '';
    	$vendredi_soir = isset($_POST['vendredi_soir']) ? $_POST['vendredi_soir'] : '';
    	
    		
    	if (isset($_POST['lundi_matin'])) {
    	$lundi_matin = 1;
    	} else {
    	$lundi_matin = 0;
    	}
    
    	
    	if (isset($_POST['lundi_soir'])) {
    	$lundi_soir = 1;
    	} else {
    	$lundi_soir = 0;
    	}
    	
    	if (isset($_POST['mardi_matin'])) {
       $mardi_matin = 1;
    	} else {
       $mardi_matin = 0;
    	}
    	
    	if (isset($_POST['mardi_soir'])) {
    	$mardi_soir = 1;
    	} else {
    	$mardi_soir = 0;
    	}
    	
    	if (isset($_POST['mercredi_matin'])) {
       $mercredi_matin = 1;
    	} else {
       $mercredi_matin = 0;
    	}
    	
    	if (isset($_POST['mercredi_soir'])) {
    	$mercredi_soir = 1;
    	} else {
    	$mercredi_soir = 0;
    	}
    	
    	if (isset($_POST['jeudi_matin'])) {
       $jeudi_matin = 1;
    	} else {
       $jeudi_matin = 0;
    	}
    	
    	if (isset($_POST['jeudi_soir'])) {
    	$jeudi_soir = 1;
    	} else {
    	$jeudi_soir = 0;
    	}
    	
    	if (isset($_POST['vendredi_matin'])) {
       $vendredi_matin = 1;
    	} else {
       $vendredi_matin = 0;
    	}
    
    	if (isset($_POST['vendredi_soir'])) {
    	$vendredi_soir = 1;
    	} else {
    	$vendredi_soir = 0;
    	}
    				
    	$req = 'INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                VALUES = ""';
    	
    	$stmt = $bdd->prepare($req);					
    	$stmt->bindParam(':lundi_matin',$lundi_matin);
    	$stmt->bindParam(':lundi_soir',$lundi_soir);
    	$stmt->bindParam(':mardi_matin',$mardi_matin);
    	$stmt->bindParam(':mardi_soir',$mardi_soir);
    	$stmt->bindParam(':mercredi_matin',$mercredi_matin);
    	$stmt->bindParam(':mercredi_soir',$mercredi_soir);
    	$stmt->bindParam(':jeudi_matin',$jeudi_matin);
    	$stmt->bindParam(':jeudi_soir',$jeudi_soir);
    	$stmt->bindParam(':vendredi_matin',$vendredi_matin);
    	$stmt->bindParam(':vendredi_soir',$vendredi_soir);
    	
    	$req = array(":lundi_matin"=> $lundi_matin,":lundi_soir"=> $lundi_soir,":mardi_matin"=> $mardi_matin, ":mardi_soir" => $mardi_soir, ":mercredi_matin" => $mercredi_soir, ":jeudi_matin" => $jeudi_matin, ":jeudi_soir" => $jeudi_soir, ":vendredi_matin" => $vendredi_matin, ":vendredi_soir" => $vendredi_soir);
    	
    	$stmt->execute();
    
    1. Modérateur
      Pourquoi est-ce que tu as :
      $lundi_matin = isset($_POST['lundi_matin']) ? $_POST['lundi_matin'] : '';
       $mardi_matin = isset($_POST['mardi_matin']) ? $_POST['mardi_matin'] : '';
       $mercredi_matin = isset($_POST['mercredi_matin']) ? $_POST['mercredi_matin'] : '';
       $jeudi_matin = isset($_POST['jeudi_matin']) ? $_POST['jeudi_matin'] : '';
       $vendredi_matin = isset($_POST['vendredi_matin']) ? $_POST['vendredi_matin'] : '';
       
       $lundi_soir = isset($_POST['lundi_soir']) ? $_POST['lundi_soir'] : '';
       $mardi_soir = isset($_POST['mardi_soir']) ? $_POST['mardi_soir'] : '';
       $mercredi_soir = isset($_POST['mercredi_soir']) ? $_POST['mercredi_soir'] : '';
       $jeudi_soir = isset($_POST['jeudi_soir']) ? $_POST['jeudi_soir'] : '';
       $vendredi_soir = isset($_POST['vendredi_soir']) ? $_POST['vendredi_soir'] : '';
       
      

      ET ça :
      if (isset($_POST['lundi_matin'])) {
       $lundi_matin = 1;
       } else {
       $lundi_matin = 0;
       }
      
       
       if (isset($_POST['lundi_soir'])) {
       $lundi_soir = 1;
       } else {
       $lundi_soir = 0;
       }
       
       if (isset($_POST['mardi_matin'])) {
         $mardi_matin = 1;
       } else {
         $mardi_matin = 0;
       }
       
       if (isset($_POST['mardi_soir'])) {
       $mardi_soir = 1;
       } else {
       $mardi_soir = 0;
       }
       
       if (isset($_POST['mercredi_matin'])) {
         $mercredi_matin = 1;
       } else {
         $mercredi_matin = 0;
       }
       
       if (isset($_POST['mercredi_soir'])) {
       $mercredi_soir = 1;
       } else {
       $mercredi_soir = 0;
       }
       
       if (isset($_POST['jeudi_matin'])) {
         $jeudi_matin = 1;
       } else {
         $jeudi_matin = 0;
       }
       
       if (isset($_POST['jeudi_soir'])) {
       $jeudi_soir = 1;
       } else {
       $jeudi_soir = 0;
       }
       
       if (isset($_POST['vendredi_matin'])) {
         $vendredi_matin = 1;
       } else {
         $vendredi_matin = 0;
       }
      
       if (isset($_POST['vendredi_soir'])) {
       $vendredi_soir = 1;
       } else {
       $vendredi_soir = 0;
       }
      

      ?????

      Si ton but est d'avoir des 1 ou des 0 en fonction des variables ....
      il suffit d'écrire la récupération de tes variables ainsi :
      $lundi_matin = isset($_POST['lundi_matin']) ? 1 : 0;
      $mardi_matin = isset($_POST['mardi_matin']) ? 1 : 0;
      $mercredi_matin = isset($_POST['mercredi_matin']) ? 1 : 0;
      $jeudi_matin = isset($_POST['jeudi_matin']) ? 1 : 0;
      $vendredi_matin = isset($_POST['vendredi_matin'])? 1 : 0;
      
      $lundi_soir = isset($_POST['lundi_soir']) ? 1 : 0;
      $mardi_soir = isset($_POST['mardi_soir']) ? 1 : 0;
      $mercredi_soir = isset($_POST['mercredi_soir']) ? 1 : 0;
      $jeudi_soir = isset($_POST['jeudi_soir']) ? 1 : 0;
      $vendredi_soir = isset($_POST['vendredi_soir']) ? 1 : 0;
      
      


      Ensuite... tu as une erreur dans ta requête....
      $req = 'INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                  VALUES = ""';
      

      ..... tu dois mettre après le VALUES ... les variables à insérer.....

      ....puis... tu dois choisir entre :
      $stmt->bindParam(':lundi_matin',$lundi_matin);
       $stmt->bindParam(':lundi_soir',$lundi_soir);
       $stmt->bindParam(':mardi_matin',$mardi_matin);
       $stmt->bindParam(':mardi_soir',$mardi_soir);
       $stmt->bindParam(':mercredi_matin',$mercredi_matin);
       $stmt->bindParam(':mercredi_soir',$mercredi_soir);
       $stmt->bindParam(':jeudi_matin',$jeudi_matin);
       $stmt->bindParam(':jeudi_soir',$jeudi_soir);
       $stmt->bindParam(':vendredi_matin',$vendredi_matin);
       $stmt->bindParam(':vendredi_soir',$vendredi_soir);
      

      ET

      $req = array(":lundi_matin"=> $lundi_matin,":lundi_soir"=> $lundi_soir,":mardi_matin"=> $mardi_matin, ":mardi_soir" => $mardi_soir, ":mercredi_matin" => $mercredi_soir, ":jeudi_matin" => $jeudi_matin, ":jeudi_soir" => $jeudi_soir, ":vendredi_matin" => $vendredi_matin, ":vendredi_soir" => $vendredi_soir);
      


      ...pas les deux !

      Bref... une fois corrigé.. tu devrais avoir :
      <?php
      error_reporting(E_ALL);
      
      //connexion à la BDD
      require_once "cnxBDD.php";
      
      
      //Récupération des variables
      $lundi_matin = isset($_POST['lundi_matin']) ? 1 : 0;
      $mardi_matin = isset($_POST['mardi_matin']) ? 1 : 0;
      $mercredi_matin = isset($_POST['mercredi_matin']) ? 1 : 0;
      $jeudi_matin = isset($_POST['jeudi_matin']) ? 1 : 0;
      $vendredi_matin = isset($_POST['vendredi_matin'])? 1 : 0;
      
      $lundi_soir = isset($_POST['lundi_soir']) ? 1 : 0;
      $mardi_soir = isset($_POST['mardi_soir']) ? 1 : 0;
      $mercredi_soir = isset($_POST['mercredi_soir']) ? 1 : 0;
      $jeudi_soir = isset($_POST['jeudi_soir']) ? 1 : 0;
      $vendredi_soir = isset($_POST['vendredi_soir']) ? 1 : 0;
      
      try{
       //preparation de la requete
        $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                VALUES = (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
          
        $a_data = array(":lundi_matin"=> $lundi_matin
                                   ,":lundi_soir"=> $lundi_soir
                                   ,":mardi_matin"=> $mardi_matin
                                   , ":mardi_soir" => $mardi_soir
                                   , ":mercredi_matin" => $mercredi_soir
                                   , ":jeudi_matin" => $jeudi_matin
                                   , ":jeudi_soir" => $jeudi_soir
                                   , ":vendredi_matin" => $vendredi_matin
                                   , ":vendredi_soir" => $vendredi_soir
                                   , ":mercredi_soir" => $mercredi_soir);
       
        //Exécution de la requête 
        $stmt = $bdd->prepare($sql); 
        $stmt->execute($a_data);
      
      } catch (Exception $e){
          //en cas de pb dans la requete
          echo 'Erreur : ' . $e->getMessage());
          echo "<br> Dans la requête :".$sql;
          echo "<br> Variables :<br><pre>";
          print_r($a_data);
          echo "</pre>";
          exit();
      }
      


      Le bloc TRY / CATCH permettant d'afficher le détail de l'erreur en cas de problème dans l'éxécution de la requête....
      1. Merci pour l'aide, franchement je m'en sors pas...mais j'ai toujours ça :

        Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projet\reservation_garderie.php on line 34
      2. Modérateur
        @lecjoh59Compare la requête avec le nombre de variables dans l'Array. .. tu verras qu'il en manque une...
        mercredi_soir ...
      3. Merci j'ai rajouter, il valide mais toujours pas d'enregistrements.
      4. Modérateur
        Il valide ... c'est à dire ?

        Tu peux faire un print de $a_data pour voir ce qu'il contient ??
      5. Quand je fais un print de a_data y'a rien qui s'affiche.
    2. error_reporting(E_ALL);
      
      require_once "cnxBDD.php";
      
      $lundi_matin = isset($_POST['lundi_matin']) ? 1 : 0;
      $mardi_matin = isset($_POST['mardi_matin']) ? 1 : 0;
      $mercredi_matin = isset($_POST['mercredi_matin']) ? 1 : 0;
      $jeudi_matin = isset($_POST['jeudi_matin']) ? 1 : 0;
      $vendredi_matin = isset($_POST['vendredi_matin'])? 1 : 0;
      
      $lundi_soir = isset($_POST['lundi_soir']) ? 1 : 0;
      $mardi_soir = isset($_POST['mardi_soir']) ? 1 : 0;
      $mercredi_soir = isset($_POST['mercredi_soir']) ? 1 : 0;
      $jeudi_soir = isset($_POST['jeudi_soir']) ? 1 : 0;
      $vendredi_soir = isset($_POST['vendredi_soir']) ? 1 : 0;
      
      try{
      
        $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                VALUES = (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
          
        $a_data = array(":lundi_matin"=> $lundi_matin
      							 ,":lundi_soir"=> $lundi_soir
                                   ,":mardi_matin"=> $mardi_matin
                                   , ":mardi_soir" => $mardi_soir
                                   , ":mercredi_matin" => $mercredi_matin
      							 , ":mercredi_soir" => $mercredi_soir
                                   , ":jeudi_matin" => $jeudi_matin
                                   , ":jeudi_soir" => $jeudi_soir
                                   , ":vendredi_matin" => $vendredi_matin
                                   , ":vendredi_soir" => $vendredi_soir);
       
        $stmt = $bdd->prepare($sql); 
        $stmt->execute($a_data);
        print_r($a_data);
        
        
      } catch (Exception $e){
          echo 'Erreur : ' . $e->getMessage();
          echo "<br> Dans la requête :".$sql;
          echo "<br> Variables :<br><pre>";
          print_r($a_data);
      	echo '$a_data';
          echo "</pre>";
          exit();
      }
      
      1. Il affiche ça :

        Array ( [:lundi_matin] => 0 [:lundi_soir] => 0 [:mardi_matin] => 0 [:mardi_soir] => 0 [:mercredi_matin] => 0 [:mercredi_soir] => 0 [:jeudi_matin] => 1 [:jeudi_soir] => 1 [:vendredi_matin] => 0 [:vendredi_soir] => 0 )
      2. Modérateur
        Donc tu as bien les bonnes valeurs....
        alors qu'est ce qui ne marche pas ??
      3. Oui j'ai les bonnes valeurs mais elles s'enregistrent pas dans la table.
      4. ?
      5. Modérateur
        Essayes ça :
        
        try{
        
          $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                  VALUES = (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
            
          $a_data = array(":lundi_matin"=> $lundi_matin
        							 ,":lundi_soir"=> $lundi_soir
                                     ,":mardi_matin"=> $mardi_matin
                                     , ":mardi_soir" => $mardi_soir
                                     , ":mercredi_matin" => $mercredi_matin
        							 , ":mercredi_soir" => $mercredi_soir
                                     , ":jeudi_matin" => $jeudi_matin
                                     , ":jeudi_soir" => $jeudi_soir
                                     , ":vendredi_matin" => $vendredi_matin
                                     , ":vendredi_soir" => $vendredi_soir);
         
        
           $stmt = $bdd->prepare($sql); 
           $stmt->execute($a_data);
           $id =  $bdd->lastInsertId();
             
            if($id){
             echo "<br> Enregistrement effectué !.  ID = ".$id;
            }else{
             echo "<br> Erreur lors de l'enregistrement !"
             print_r($a_data);
            }
          
        } catch (Exception $e){
            echo 'Erreur : ' . $e->getMessage();
            echo "<br> Dans la requête :".$sql;
            echo "<br> Variables :<br><pre>";
            print_r($a_data);
        	echo '$a_data';
            echo "</pre>";
            exit();
        }
        
        
    3. Salut, ça me fait ça :

      Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projet\reservation_garderie.php on line 44

      error_reporting(E_ALL);
      
      require_once "cnxBDD.php";
      
      $lundi_matin = isset($_POST['lundi_matin']) ? 1 : 0;
      $mardi_matin = isset($_POST['mardi_matin']) ? 1 : 0;
      $mercredi_matin = isset($_POST['mercredi_matin']) ? 1 : 0;
      $jeudi_matin = isset($_POST['jeudi_matin']) ? 1 : 0;
      $vendredi_matin = isset($_POST['vendredi_matin'])? 1 : 0;
      
      $lundi_soir = isset($_POST['lundi_soir']) ? 1 : 0;
      $mardi_soir = isset($_POST['mardi_soir']) ? 1 : 0;
      $mercredi_soir = isset($_POST['mercredi_soir']) ? 1 : 0;
      $jeudi_soir = isset($_POST['jeudi_soir']) ? 1 : 0;
      $vendredi_soir = isset($_POST['vendredi_soir']) ? 1 : 0;
      
      try{
      
        $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                VALUES = (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
          
        $a_data = array(":lundi_matin"=> $lundi_matin
              ,":lundi_soir"=> $lundi_soir
                                   ,":mardi_matin"=> $mardi_matin
                                   , ":mardi_soir" => $mardi_soir
                                   , ":mercredi_matin" => $mercredi_matin
              , ":mercredi_soir" => $mercredi_soir
                                   , ":jeudi_matin" => $jeudi_matin
                                   , ":jeudi_soir" => $jeudi_soir
                                   , ":vendredi_matin" => $vendredi_matin
                                   , ":vendredi_soir" => $vendredi_soir);
       
      
         $stmt = $bdd->prepare($sql); 
         $stmt->execute($a_data);
         $id =  $bdd->lastInsertId();
           
          if($id){
           echo "<br> Enregistrement effectué !.  ID = ".$id;
          }else{
           echo "<br> Erreur lors de l'enregistrement !";
           //print_r($a_data);
       } catch (Exception $e){
          echo 'Erreur : ' . $e->getMessage();
          echo "<br> Dans la requête :".$sql;
          echo "<br> Variables :<br><pre>";
          print_r($a_data);
       echo '$a_data';
          echo "</pre>";
          exit();
      }
      
      1. Modérateur
        Tu n'es pas capable de fqire un coper/coller du code que je t'ai donné sans faire d'erreur ?? Oo
      2. En copiant collant ton code, il y a cette erreur.
      3. Modérateur
        @lecjoh59Non pas possible ...
        le code que toi tu nous montres contient une erreur ... ( une accolade manquante après le if / else ) ... accolade pourtant présente dans le code que je t'ai donné.

        <?php
        
        try{
        
          $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                  VALUES = (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
            
          $a_data = array(":lundi_matin"=> $lundi_matin
                        ,":lundi_soir"=> $lundi_soir
                        ,":mardi_matin"=> $mardi_matin
                        , ":mardi_soir" => $mardi_soir
                        , ":mercredi_matin" => $mercredi_matin
                        , ":mercredi_soir" => $mercredi_soir
                        , ":jeudi_matin" => $jeudi_matin
                        , ":jeudi_soir" => $jeudi_soir
                        , ":vendredi_matin" => $vendredi_matin
                        , ":vendredi_soir" => $vendredi_soir
                );
         
        
           $stmt = $bdd->prepare($sql); 
           $stmt->execute($a_data);
           $id =  $bdd->lastInsertId();
             
           if($id){
             echo "<br> Enregistrement effectué !.  ID = ".$id;
           }else{
             echo "<br> Erreur lors de l'enregistrement !";
             print_r($a_data);
           }
          
        } catch (Exception $e){
           echo 'Erreur : ' . $e->getMessage();
           echo "<br> Dans la requête :".$sql;
           echo "<br> Variables :<br><pre>";
           print_r($a_data);
          echo '$a_data';
           echo "</pre>";
           exit();
        }
        
      4. Le code passe mais à l'éxécution il me met :

        Erreur lors de l'enregistrement !Array ( [:lundi_matin] => 1 [:lundi_soir] => 0 [:mardi_matin] => 0 [:mardi_soir] => 1 [:mercredi_matin] => 0 [:mercredi_soir] => 0 [:jeudi_matin] => 0 [:jeudi_soir] => 0 [:vendredi_matin] => 0 [:vendredi_soir] => 0 )
      5. Juste par curiosité, le champ ID est un champ auto incrémenté ? ou pas.
    4. Modérateur
      Et donc... sans le "=" en trop ... le code serait :
      try{
      
        $sql = "INSERT INTO garderie (lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)
                VALUES  (:lundi_matin, :lundi_soir, :mardi_matin, :mardi_soir, :mercredi_matin, :mercredi_soir, :jeudi_matin, :jeudi_soir, :vendredi_matin, :vendredi_soir)";
          
        $a_data = array(":lundi_matin"=> $lundi_matin
                      ,":lundi_soir"=> $lundi_soir
                      ,":mardi_matin"=> $mardi_matin
                      , ":mardi_soir" => $mardi_soir
                      , ":mercredi_matin" => $mercredi_matin
                      , ":mercredi_soir" => $mercredi_soir
                      , ":jeudi_matin" => $jeudi_matin
                      , ":jeudi_soir" => $jeudi_soir
                      , ":vendredi_matin" => $vendredi_matin
                      , ":vendredi_soir" => $vendredi_soir
              );
       
      
         $stmt = $bdd->prepare($sql); 
         $stmt->execute($a_data);
         $id =  $bdd->lastInsertId();
           
         if($id){
           echo "<br> Enregistrement effectué !.  ID = ".$id;
         }else{
           echo "<br> Erreur lors de l'enregistrement !";
           print_r($a_data);
         }
        
      } catch (Exception $e){
         echo 'Erreur : ' . $e->getMessage();
         echo "<br> Dans la requête :".$sql;
         echo "<br> Variables :<br><pre>";
         print_r($a_data);
        echo '$a_data';
         echo "</pre>";
         exit();
      }
      

      1. Modérateur
        Je ne l'avais pas vu en recopiant ton code..... :-(
    5. Merci pour l'aide c'est super sympa !
      1. Modérateur
        Si la question est résolue..
        Merci de ne pas oublier de clôturer le sujet
        (en cliquant sur le lien "Marquer comme résolu" qui se trouve sous le titre de la question)

        Cordialement,
        Jordane
    6. Par contre j'ai un autre petit problème, vous pouvez regarder ?
      1. Modérateur

        j'ai un autre petit problème

        Autre Problème = Ouverture NOUVELLE discussion. (après avoir mis celle la en résulue )
        cela facilite la recherche sur le forum.

        Merci.
    7. J'ai un petit problème avec un bouton de redirection, j'aimerai quand on clique sur le bouton Enregistrer la page d'après soit index2.html

      De la page connexion vers index2

      Voici les codes de connexion.html :

      <head>
              <meta charset="utf-8" />
          </head>
          <body>
          
      	<form method="POST" action="connexion.php">
      	<p>
             <label for="username">Pseudo :</label>
             <input type="text" name="username" id="username" />
             
             <br></br>
      	   
             <label for="password">Mot de passe :</label>
             <input type="text" name="password" id="password" />
             
      	   <br></br>
      	   
             <label for="password2"> Retapez votre mot de passe :</label>
             <input type="text" name="password2" id="password2" />
      
      	   <br></br>
      	   
      		<input type="submit" name="submit" value="Envoyer" >
      	   
      	</p>
      
      	</form>
        
          </body>
      


      connexion.php :

      error_reporting(E_ALL);
      
      if(isset($_POST['submit']))
      {
      $username =htmlentities(trim($_POST['username']));	
      $password =htmlentities(trim($_POST['password']));	
      $password2 =htmlentities(trim($_POST['password2']));		
      	if($username&&$password&&$password2)
      	{
      		if($password==$password2)
      		{
      		$password = md5($password);	
      		$bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8', 'root', '');
      		try
      		{
      			$bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8', 'root', '');
      		}
      		catch (Exception $e)
      		{
              die('Erreur : ' . $e->getMessage());
      		}		
      		mysql_select_db('projet');
      		$reg = mysql_query("SELECT * FROM connexion WHERE username='$username'");
      		
      		$req = $bdd->prepare("INSERT INTO connexion (username, password) VALUES (:username, :password)");
      		$req->execute(array("username" => $username, "password" => $password));
      		
      		
      		die("Inscription terminée <ahref=connexion.php connectez</a>vous");
      		}else echo"Ce pseudo n'est pas disponible";
      		}else echo "Les deux passwords doivent être identiques";
      	};