Des inserts qui se font deux fois
Bonjour,
J'ai un formulaire assez simple gérant des enregistrements dans une table 'sondages'.
Je n'arrive pas à m'en sortir car à chaque 'insert' j'en récupère 2 !!!!
j'ai donc besoin d'aide.
Voici la structure de ma table :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| -- Adminer 4.8.0 MySQL 5.7.31 dump
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `sondages`;
CREATE TABLE `sondages` (
`id_sondage` int(6) NOT NULL AUTO_INCREMENT,
`id_initiateur` int(11) DEFAULT NULL,
`nom_sondage` varchar(64) NOT NULL,
`nbre_questions` int(3) NOT NULL,
`cible_sondage` varchar(64) NOT NULL,
`date_creation` date NOT NULL,
`date_cloture` date NOT NULL,
PRIMARY KEY (`id_sondage`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 2021-06-12 16:36:37 |
et voici le code de mon formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| <?php session_start();
// Ges_Sondage... inspiré de Ges_Pelerinages et de SStatuts pour le sans_destroy 05/06/2021
// (et aussi de Ges_Agenda pour le sans_destroy)
include_once ("En_tete_HV.php");
$id_personne_log = $_SESSION['id_personne_log']; echo "id_personne_log : ".$id_personne_log; echo "<br>"; echo "<br>"; echo "<br>";
$code_site = $_SESSION['code_site']; // echo "code_site : ".$code_site; echo"<br>";
$nom_site = $_SESSION['nom_site'];
$domaine = $_SERVER['SERVER_NAME'];
$today = date("d/m/Y"); // d à H:i:s");
echo '<div class="no_print" >'; // Pas d'impression
echo " <input type=button class='btn' style='font-size:100%; color:black;' onclick=window.location.href='Ges_Tables_Parametres_HV.php'; value='Retour au Menu des PARAMETRES' />"; echo "<br>";
echo "<br>";
echo '</div>'; // On imprimera
echo " <h1> Gestion des SONDAGES </h1> "; // au : ".$today."</h1> ";
//*********************************************************************************
// Liste de TOUS les SONDAGES ***************************************************
//*********************************************************************************
?>
<table>
<tr align='center'>
<!-- <th> N° </th> -->
<th> id_sondage </th>
<th> Initiateur </th>
<th> NOM_du_Sondage </th>
<th> Cible_sondage </th>
<th> Nb_quest </th>
<th> Date_Création </th>
<th> Date_Clôture </th>
<!-- <th> Actions </th> -->
</tr>
<?php
$num = 0;
$my_sql = "SELECT * FROM sondages ORDER BY id_sondage ASC ";
$result = mysqli_query($my_cnx, $my_sql) or die ('Erreur ici vers 133 : '.mysqli_error($my_cnx) );
$nb_sondages = mysqli_num_rows($result);
while ($ligne = mysqli_fetch_assoc($result)) {
$num ++;
echo "<tr>";
// echo "<td>"."   ".$num. "</td>";
echo "<td>"."   ".$ligne['id_sondage']. "</td>";
echo "<td>"."   ".$ligne['id_initiateur']. "</td>";
echo "<td>"."   ".$ligne['nom_sondage']. "</td>";
echo "<td>"."   ".$ligne['cible_sondage']. "</td>";
echo "<td>"."   ".$ligne['nbre_questions']. "</td>";
echo "<td>"."   ".$ligne['date_creation']. "</td>";
echo "<td>"."   ".$ligne['date_cloture']. "</td>";
// echo "<td>";
// echo "<input type=button class='btn' style='font-size:100%; color:black;' onclick=window.location.href='Ges_Sondages_HV.php?voir_detail=$ligne[id_sondage]'; value='Détails'/> ";
// echo "<input type=button class='btn' style='font-size:100%; color:black;' onclick=window.location.href='Ges_Sondages_HV.php?modif_sondage=$ligne[id_sondage]'; value='Modifier'/> ";
// echo "<input type=button class='btn' style='font-size:100%; color:black;' onclick=window.location.href='Ges_Sondages_HV.php?suppr_sondage=$ligne[id_sondage]'; value='Supprimer'/> </center> ";
// echo "</td>";
echo "</tr>";
}
echo "</table>";
echo " <h3> Il y a ".$nb_sondages." Sondages. </h3> "; echo "<br>";
echo " <form name='form1' method='post' action='Ges_Sondages_HV.php' > ";
//echo " <input type='submit' name='button' class='btn' style='font-size:100%; color:black;' size='40' value='ABANDON' > ";
echo "        ";
echo " <input type='submit' name='button' class='btn' style='font-size:100%; color:black;' size='40' value='Créer un NOUVEAU Sondage' > ";
echo "        ";
echo ' <input class="btn" id="impression" name="impression" type="button" style="font-size:100%; color:black;" onclick="imprimer_page()" value="Imprimer CETTE liste " /> '."    ";
if(isset($_POST['button'])) {
echo "<br>"; echo "<br>"; echo"valeur du POST-button : ".$_POST['button'];
switch ($_POST['button']) {
case "Créer un NOUVEAU Sondage" :
echo " <form name='form1' method='post' action='Ges_Sondages_HV.php' > ";
echo "<br>"; echo "<br>"; echo "<br>";echo "<br>"; echo "<br>";
echo " CREATION d'un NOUVEAU SONDAGE"; echo "<br>"; echo "<br>";
?>
<table>
<tr align='center'> <th> NOM du Sondage </th> <th> nbre_questions </th> <th> Cible_Sondage </th> <th> date_creation </th> <th> Date_Clôture </th> </tr>
<tr>
<td> <input name="nom_sondage" autofocus required size="30" type="text" > </td>
<td> <input name="nbre_questions" size="10" type="number" > </td>
<td> <input name="cible_sondage" size="30" type="text" > </td>
<td> <input name="date_creation" size="10" type="date" > </td>
<td> <input name="date_cloture" size="10" type="date" > </td>
</tr>
</table>
<?php
echo "<br>";
echo " <input type=button class='btn' style='font-size:100%; color:black;' onclick=window.location.href='Ges_Sondages_HV.php'; value='ABANDON' > ";
echo "        ";
echo " <input type='submit' name='button' class='btn' style='font-size:100%; color:black;' size='40' value='VALIDER ce NOUVEAU Sondage' > ";
echo "<br>"; echo "<br>"; echo "<br>";
echo "</form>";
exit;
break;
case "VALIDER ce NOUVEAU Sondage" :
echo "<br>"; echo "<br>";
$nom_sondage = $_POST['nom_sondage']; $nom_sondage = isset($nom_sondage)? addslashes($nom_sondage) : ''; // gestion des apostrophes dans le cas des 'Sables d'Olonne' 04_05_2021
$nbre_questions = $_POST['nbre_questions'];
$cible_sondage = $_POST['cible_sondage'];
$date_creation = $_POST['date_creation'];
$date_cloture = $_POST['date_cloture'];
$id_initiateur = $id_personne_log;
$my_sql = "INSERT INTO sondages (id_initiateur, nom_sondage, nbre_questions, cible_sondage, date_creation, date_cloture) VALUES ( '{$id_initiateur}', '{$nom_sondage}', '{$nbre_questions}', '{$cible_sondage}', '{$date_creation}', '{$date_cloture}' )";
$result = mysqli_query($my_cnx,$my_sql) or die ("Erreur en 387 : ".mysqli_error($my_cnx));
echo "<br>"; echo "<br>"; echo "texte de la requete d'insertion en base : ".$my_sql; // exit;
if (mysqli_query($my_cnx, $my_sql)) {
$message= "Création REALISEE avec SUCCES"; echo"<br>";
echo "<font color='green'> ".$message; echo"<br>";
}else{
$message = "ERREUR dans la REQUETE vers 118 : "."<br>" .$my_sql . "<br><br>" .mysqli_error($my_cnx)."<br>";
echo $message; //exit;
}
echo "<br>"; echo "<br>"; echo "OK après insertion du NOUVEL enregistrement ";
echo "<script type='text/javascript'>document.location.replace('Ges_Sondages_HV.php');</script>"; // Redirection immédiate .... ça marche chez 1&1
exit;
break;
case "ABANDON" : // Cet ABANDON va vers le Retour au Menu des Tables des Paramètres
echo "<script type='text/javascript'>document.location.replace('Ges_Tables_Parametres_HV.php');</script>"; // Redirection immédiate .... ça marche chez 1&1
//echo " <input class='btn' type=button style='font-size:150%; color:black;' onclick=window.location.href='Ges_Tables_Parametres_HV.php'; value='ABANDON' />"; echo "<br>";
exit;
break;
default:
echo "<br>";
echo "<br>";
echo " <h2> Aucun choix 'TROUVE' dans le Select 'Switch' (Vérifier l'orthographe des 'case' ) !!! </h2> ";
echo "<br>"; echo "<br>";
exit;
break;
} // Fin du switch
} // Fin du if(isset($_POST['button'])) {
?>
<script type="text/javascript">
function imprimer_page(){window.print();}
</script> |
Merci d'avance
Cordialement
Maub
1 pièce(s) jointe(s)
Complément sur Formulaire
Bonjour,
Je reviens sur mon formulaire de 'sondage' :
Suite à vos différents conseils j'ai pu mettre au point la saisie des réponses par boutons radio.
Ca marche parfaitement.
Voici le scan d'écran de saisie du sondage :
Pièce jointe 600635
Voici la partie du code qui produit cet écran :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php
// ************* Entre les 2 tableaux *************************
echo " <h5> (Pour EMETTRE votre choix, Cliquez sur le TEXTE DE LA REPONSE qui vous convient) </h5> ";
// Affichage et Saisie des réponses aux questions
echo "<table class ='questions'> ";
echo " <th> N° </th> <td> <center> Questions du sondage </td> <th colspan='3'> <center> Validez UNE des 3 réponses </th> ";
$my_sql = "SELECT * FROM sondage_questions WHERE id_sondage = ".$id_sondage." ORDER BY id_question";
$result = mysqli_query($my_cnx,$my_sql) or die ("Erreur sur la requête en ligne 150 : ".mysqli_error($my_cnx));
$nb_result = mysqli_num_rows($result);
$tab_questions_posees = [];
while($donnees = mysqli_fetch_assoc($result)) {
$num_question++;
$question_posee = $donnees['question_posee'];
$reponse_poss_1 = $donnees['reponse_poss_1'];
$reponse_poss_2 = $donnees['reponse_poss_2'];
$reponse_poss_3 = $donnees['reponse_poss_3'];
?>
<tr>
<td> <?php echo $num_question; ?> </td> <th> <?php echo $question_posee; ?> </th>
<td> <label> <input type="radio" required name="ok_reponse[<?= $num_question ?>]" value="<?= $reponse_poss_1 ?>"> <?= $reponse_poss_1 ?> </label> </td>
<td> <label> <input type="radio" required name="ok_reponse[<?= $num_question ?>]" value="<?= $reponse_poss_2 ?>"> <?= $reponse_poss_2 ?> </label> </td>
<td> <label> <input type="radio" required name="ok_reponse[<?= $num_question ?>]" value="<?= $reponse_poss_3 ?>"> <?= $reponse_poss_3 ?> </label> </td>
</tr>
<?php
$tab_questions_posees[] = $question_posee;
}
echo "</table> ";
echo " <h3> Chaque question nécessite 'obligatoiremt' UNE réponse. </h3> "; echo "<br>";
echo " <input class='btn' type=button style='font-size:100%; height:40px; color:black;' onclick=window.location.href='C_Logout_HV.php'; value=' ".htmlentities("J'efface TOUS mes Choix et je QUITTE", ENT_QUOTES)."' > ";
echo "     ";
echo " <input type='hidden' name='meteo' value='37' > ";
echo " <input type='submit' class='btn' style='font-size:100%; height:40px; color:black;' value=' Je CONFIRME mes CHOIX' /> ";
echo "<br>"; echo "<br>"; echo "<br>"; echo "<br>";
echo "</form>";
exit; |
Voici donc mon besoin :
Suite à la validation du bouton 'Je confirme mes choix', un tableau des réponses (ok_reponse) est généré, et est passé par POST, que je récupère dans la page suivante par un foreach. Tout va bien.
Je voudrais aussi par contre 'récupérer' les questions posées, origines de chacune des réponses et les retrouver soit :
- soit dans le même tableau des 'ok_reponse' en tant qu'indice supplémentaire
- soit avec dans un autre tableau (par ex. 'texte_question') qui ait le même nombre d'indices que les réponses, et que je puisse exploiter leur 'association' avec les réponses ... à la page suivante.
J'ai fait un essai avec un champ nommé 'meteo' pour tester le passage avec un hidden. Je retrouve bien ma valeur de 'meteo' à la page suivante ...
mais je ne sais pas comment passer (ou associer) les questions à chaque réponse ?
J'espère avoir été précis dans la formulation du problème et vous remercie à l'avance de vos suggestions.
Cordialement.
Maub.