problème pour enregistrer bouton radio dans BDD

Par lecjoh, il y a 10 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

Décrivez ici votre code ou ce que vous cherchez à faire

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <h1> Reservation garderie </h1> </head> <body> <table> <tr> <th> </th> <th> Lundi </th> <th> Mardi </th> <th> Mercredi </th> <th> Jeudi </th> <th> Vendredi </th> </tr> <tr> <th> Matin </th> <th> <INPUT type="checkbox" name="lundi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="mardi_soir" value="1"> </th> <th> <INPUT type="checkbox" name="mercredi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="jeudi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="vendredi_matin" value="1"> </th> </tr> <tr> <th> Soir </th> <th> <INPUT type="checkbox" name="lundi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="mardi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="mercredi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="jeudi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="vendredi_soir" value="2"> </th> </tr> </table> </body> <br></br> <INPUT TYPE="submit" NAME="submit" VALUE=" Envoyer "> <form action="traitementgarderie.php" method="post"> </html> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <?php $lundi_matin = true; $mardi_matin = true; $mercredi_matin = true; $jeudi_matin = true; $vendredi_matin = true; $lundi_matin = false; $mardi_matin = false; $mercredi_matin = false; $jeudi_matin = false; $vendredi_matin = false; error_reporting(E_ALL); if(isset($_POST['submit'])) { $bdd = new PDO('mysql:host=localhost;dbname=phplogin;charset=utf8', 'root', ''); try { $bdd = new PDO('mysql:host=localhost;dbname=phplogin;charset=utf8', 'root', ''); $req = "INSERT INTO garderie(lundi_matin) VALUES ('', :checkbox);"); $req .= "(lundi_matin, lundi_soir, mardi_matin, mardi_soir, mercredi_matin, mercredi_soir, jeudi_matin, jeudi_soir, vendredi_matin, vendredi_soir)"; $stmt = $bdd->prepare($req); $stmt->bindValue('lundi_matin',htmlentities(trim($_POST['lundi_matin'])),PDO::PARAM_STR); $stmt->bindValue('lundi_soir',htmlentities(trim($_POST['lundi_soir'])),PDO::PARAM_STR); $stmt->bindValue('mardi_matin',htmlentities(trim($_POST['mardi_matin'])),PDO::PARAM_STR); $stmt->bindValue('mardi_soir',htmlentities(trim($_POST['mardi_soir'])),PDO::PARAM_STR); $stmt->bindValue('mercredi_matin',htmlentities(trim($_POST['mercredi_matin'])),PDO::PARAM_STR); $stmt->bindValue('mercredi_soir',htmlentities(trim($_POST['mercredi_soir'])),PDO::PARAM_STR); $stmt->bindValue('jeudi_matin',htmlentities(trim($_POST['jeudi_matin'])),PDO::PARAM_STR); $stmt->bindValue('jeudi_soir',htmlentities(trim($_POST['jeudi_soir'])),PDO::PARAM_STR); $stmt->bindValue('vendredi_matin',htmlentities(trim($_POST['vendredi_matin'])),PDO::PARAM_STR); $stmt->bindValue('vendredi_soir',htmlentities(trim($_POST['vendredi_soir'])),PDO::PARAM_STR); } if(isset($_POST["lundi_matin"]) { $lundi_matin = $_POST['lundi_matin']; }else{ echo faux }; }; ?>

Ce que je veux

Enregistrer les boutons radios cochés dans la BDD

Ce que j'obtiens

Rien

12 réponses

lecjoh, il y a 10 ans

Salut, bah tout d'abord j'aimerais que les cases cochées s'enregistrent dans la BDD. En cliquant sur le bouton de soumission, rien ne se passe.

Matho13, il y a 10 ans

Premier constat, tu a ouvert la balise form à la fin au lieu de l'ouvrir au début

lecjoh, il y a 10 ans

Okok puis ?

Matho13, il y a 10 ans

salut lecjoh,
si tu pourrais nous indique le checkbox qui cause problème ou si tu peux nous indique l'erreur qui survient lorsque tu cliques sur le bouton d soumission

Matho13, il y a 10 ans

Ce que tu devrais faire normalement comme ceci (partie formulaire) est d'entourer tout ceci avec la balise <form>

<!-- à entourer avec la balise <form>--> <form> <table> <tr> <th> </th> <th> Lundi </th> <th> Mardi </th> <th> Mercredi </th> <th> Jeudi </th> <th> Vendredi </th> </tr> <tr> <th> Matin </th> <th> <INPUT type="checkbox" name="lundi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="mardi_soir" value="1"> </th> <th> <INPUT type="checkbox" name="mercredi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="jeudi_matin" value="1"> </th> <th> <INPUT type="checkbox" name="vendredi_matin" value="1"> </th> </tr> <tr> <th> Soir </th> <th> <INPUT type="checkbox" name="lundi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="mardi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="mercredi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="jeudi_soir" value="2"> </th> <th> <INPUT type="checkbox" name="vendredi_soir" value="2"> </th> </tr> </table> </form>
lecjoh, il y a 10 ans

C'est bon j'ai modifié. Le problème doit venir du traitement ?

Matho13, il y a 10 ans

Ok,
en cas de problème, n'hesites pas à le signaler

lecjoh, il y a 10 ans

Quand je fais enregistrer, rien ne se passe.

Matho13, il y a 10 ans

As-tu préciser les options suplementaires à la balise <form> comme ceci

<form action="page_traitement.php" method="post"> </form>
lecjoh, il y a 10 ans

Oui : <form action="traitementgarderie.php" method="post">

kwik, il y a 10 ans

Salut

Je tiens a signaler qu'il est possible que je dise des conneries, je débute dans le php mysql :

j'ai remarquais plusieurs erreurs dans ton traitement :

  • Tu instancies 2 fois la connexion à la base de donnée
  • Tu fais un try mais tu ne fais pas le catch
  • Ensuite pour l'insertion en base de donnée la requête n'est pas bonne (en tous cas de ce que j'ai appris y a quelques temps)
  • Dans ton else tu fais un echo faux au lieux echo "faux";
  • Ensuite après les accolade } normalement il ne doit y avoir de ;

Pour le try catch un truc dans le genre :

// Connexion à la base de donnée try { $bdd = new PDO('mysql:host=localhost;dbname=phplogin;', 'root', ''); } catch(PDOException $e) { exit($e->getMessage()); }

Pour les requetes un truc dans le genres :

$req = $bdd->prepare('INSERT INTO garderie (ta_table) VALUES(:param1, :param2)'); $req->execute(array(':param1' => $params1, ':param2' => $param2));

ou bien :

$req = $bdd->prepare('INSERT INTO garderie (ta_table) VALUES (:param1, :param2)'); $req->bindValue(':param1', $param1); $req->bindValue(':param2', $param2); $req->execute();

Il y a sans doute des erreurs je suis sur mon téléphone désolé

stockecedric, il y a 10 ans

Tu dois d'abord regarder si le bouton radio est coché puis ensuite envoyé le résultat dans ta base de donné via une requête et non envoyé la requête sans tester la radio.