problème liste déroulante avec BDD

problème liste déroulante avec BDD - PHP - Programmation

Marsh Posté le 01-02-2016 à 14:44:58    

Bonjour, j'ai une petite erreur avec mon code certainement. Mon erreur est : quand on choisit une des 3 écoles, dans la base il y a des chiffres qui s'inscrivent et non le nom des écoles. Voici mes codes html et php :
 

Code :
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <title>Inscription enfant</title>
  6.     </head>
  7.     <body>
  8.     <form method="POST" action="inscription_enfant.php">
  9. <p>
  10.        <label for="nom">Nom :</label>
  11.        <input type="text" name="nom" id="nom" />
  12.      
  13.        <br></br>
  14.  
  15.        <label for="prenom">Prenom :</label>
  16.        <input type="text" name="prenom" id="prenom" />
  17.    <br></br>
  18.  
  19.    <label for="nom_ecole">Ecole :</label>
  20.  <select id='nom_ecole' name='nom_ecole' onChange='nom_ecole();'>
  21.  <option value="Charles Perrault">Charles Perrault</option>
  22.  <option value="Charles Robert">Charles Robert</option>
  23.  <option value="Groupe Champ Fleuri">Groupe Champ Fleuri</option>
  24.  </select>
  25.  <br></br>
  26. <FORM>
  27.  <INPUT TYPE="submit" NAME="submit" VALUE=" Envoyer ">
  28. </FORM>
  29. </body>
  30. </html>
  31. <?php
  32.  
  33. error_reporting(E_ALL);
  34.  
  35. $nom = isset($_POST['nom']) ? htmlentities(trim($_POST['nom'])) : '';
  36. $prenom = isset($_POST['prenom']) ? htmlentities(trim($_POST['prenom'])) : '';
  37. $nom_ecole = isset($_POST['nom_ecole']) ? htmlentities(trim($_POST['nom_ecole'])) : '';
  38.  
  39.     if(isset($_POST['submit']))
  40.     {
  41.     $bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8', 'root', '');
  42.       try
  43.     {
  44.         $bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8', 'root', '');
  45.         $req = "INSERT INTO inscription_enfant (nom, prenom, nom_ecole)";
  46.         $req .= " VALUES ";
  47.         $req .= "(:nom, :prenom, :nom_ecole)";
  48.         $stmt = $bdd->prepare($req);
  49.         $stmt->bindValue('nom',$nom,PDO::PARAM_STR);
  50.         $stmt->bindValue('prenom',$prenom,PDO::PARAM_STR);
  51.  $stmt->bindValue('nom_ecole',$nom_ecole,PDO::PARAM_STR);
  52.         if (!$stmt->execute()) {
  53.             throw new PDOException('insertion impossible');
  54.         }
  55.     }
  56.     catch (Exception $e){
  57.         die('Erreur : ' . $e->getMessage());
  58.     }
  59. }
  60.     ?>


 
 
Cela ne viendrait t-il pas de la base ?
Merci d'avance


Message édité par Harkonnen le 02-02-2016 à 09:23:07
Reply

Marsh Posté le 01-02-2016 à 14:44:58   

Reply

Marsh Posté le 02-02-2016 à 09:28:22    

J'ai reformaté ton code pour plus de lisibilité. La prochaine fois que tu postes du code, utilise les balises appropriées.
 
En ce qui concerne ton souci, j'ai lu ton code rapidement et j'ai constaté que tu utilisais la balise <form> 2 fois (ligne 9 et ligne 30). Celle de la ligne 30 est totalement inutile. Si ton souci n'est pas résolu en la supprimant, alors fais un var_dump() de tes variables transmises en POST (nom, prénom et nom_ecole) pour voir si tu les reçois correctement.


---------------
J'ai un string dans l'array (Paris Hilton)
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed