Problème d'insertion de données
Posté : 15 févr. 2017, 11:16
Bonjour,
J'essaye d'insérrer plusieurs enregistrments en même temps, ces données viennent d'un formulaire, dans la page de validation je dois récupérer les index du tableau et là je n'y arrive pas, dans ma table il me met x fois la même personne mon problème est que je ne sais pas récupérer les index du tableau
J'essaye d'insérrer plusieurs enregistrments en même temps, ces données viennent d'un formulaire, dans la page de validation je dois récupérer les index du tableau et là je n'y arrive pas, dans ma table il me met x fois la même personne mon problème est que je ne sais pas récupérer les index du tableau
foreach($_POST['id_perso'] as $i => $v) {
$req->execute(array(
':id_perso'=>$v,
':id_ligne_voyage'=>$_POST['id_ligne_voyage'], // ici je devrais avoir l'index et non pas $_POST
Enfin j'espère que ce message est clair...Merci de votre aide.
<?php
$param_Voyage = $bd->query('SELECT id, id_ligne_voyage, destination, DATE_FORMAT(date_debut, "%d/%m/%Y") as date_debut,
DATE_FORMAT(date_fin, "%d/%m/%Y") as date_fin,
nbre_place, montant_g, piece
FROM param_voyage WHERE id_ligne_voyage ="' . $_SESSION['id_voy']. '" ');
$data_voyage = $param_Voyage->fetch();
echo "<p><B>Voyage sélectionné " .$data_voyage['destination'].
" du " .$data_voyage['date_debut']. " au " .$data_voyage['date_fin']." Nbre de places " .$data_voyage['nbre_place']. " Montant global " .number_format($data_voyage['montant_g'], 2, ',', ' ')." €</B></p>";
$param_sous_Voyage = $bd->query('SELECT id_voyage, id_ligne, tranche, montant_t
FROM param_sous_voyage WHERE id_voyage ="' . $data_voyage['id']. '" ORDER BY tranche ASC ');
while ($data_sous_voyage = $param_sous_Voyage->fetch())
{
echo "<p><B>Référence id voyage " .$data_sous_voyage['id_voyage'].
" " .$data_sous_voyage['tranche']. " Montant " .number_format($data_sous_voyage['montant_t'], 2, ',', ' ')." €</B></p>";
}
?>
<form method="post" action="select_voyageur.php">
<p>
<label for="profil">Veuillez sélectionner un membre...</label>
<select id="matricule" name="matricule">
<option selected="selected"></option>
<?php
$select_membre = $bd->query('SELECT matricule, id, nom, prenom FROM membre ORDER BY nom ASC');
while ($liste_membre = $select_membre->fetch())
{
echo '<option value="'.$liste_membre["matricule"].'""'.stripslashes(strtoupper($liste_membre["nom"])).'""'.stripslashes($liste_membre["prenom"]).'">'. stripslashes(strtoupper($liste_membre["nom"])).' '. stripslashes($liste_membre["prenom"]).'</option>';
}
session_start();
$_SESSION['id_mat']=$_POST["matricule"];
?>
</select>
<input type="submit" value="Rechercher" />
</p>
</form>
<?php
if (isset($_POST['matricule']))
{
echo "<p>Profil sélectionné " . stripslashes($_SESSION['id_mat'])."</p>";
?>
<?php
$Param_voyageur = $bd->query('SELECT
membre.id as "id_perso",
"Mem" as "type",
membre.matricule as "matricule",
membre.civilite as "civilite",
membre.nom as "nom",
membre.nom_j_fille as "nom_j",
membre.prenom as "prenom",
DATE_FORMAT(membre.date_n, "%d/%m/%Y") as "date_n",
nationalite_sal as nationalite,
"Oui" as "select"
FROM membre where membre.matricule="'.$_SESSION['id_mat'].'"
UNION
SELECT
membre.id as "id_perso",
"Conj" as "Type",
membre.matricule as "matricule",
membre.civilite_conj as "civilite",
membre.nom_conj as "nom",
membre.jeune_fille_conj as "nom_j",
membre.prenom_conj as "prenom",
DATE_FORMAT(membre.naissance_conj, "%d/%m/%Y") as "date_n",
nationalite_conj as nationalite,
"Oui" as "select"
FROM membre where membre.matricule="'.$_SESSION['id_mat'].'"
UNION
SELECT
enfant.id as "id_perso",
"Enf" as "type",
enfant.matricule as "matricule",
enfant.civilite_enfant as "civilite",
enfant.nom_enfant as "nom",
"" as "nom_j",
enfant.prenom_enfant as "prenom",
DATE_FORMAT(enfant.naissance_enfant, "%d/%m/%Y") as "date_n",
nationalite_enf as nationalite,
"Oui" as "select"
FROM enfant where enfant.matricule="'.$_SESSION['id_mat'].'"
');
while ($data_voyageur = $Param_voyageur->fetch())
{
echo '
<form name="insertion" action="ex_insert_voyageur.php" method="POST">
<table>
<tr>
<th>Type</th>
<th>Civilité</th>
<th>Nom</th>
<th>Nom jeune fille</th>
<th>Prénom</th>
<th>Né(e) le</th>
<th>Chambre</th>
<th>N°</th>
<th>Tranche</th>
<th>Info</th>
<th>Nationalité</th>
<th>Sexe</th>
<th>Document</th>
<th></th>
</th>
<tbody>
</tr>
<tr>
<input type="text" id="id_perso" name="id_perso[]" size="4" maxlength="6" value="'.$data_voyageur["id_perso"].$data_voyageur["type"].'" >
<input type="HIDDEN" id="id_ligne_voyage" name="id_ligne_voyage[]" size="20" maxlength="15" value="'.$data_voyage["id_ligne_voyage"].'" >
<input type="HIDDEN" id="destination" name="destination[]" size="20" maxlength="15" value="'.$data_voyage["destination"].'" >
<td><input type="text" id="type" name="type[]" size="4" maxlength="3" value="'.$data_voyageur["type"] .'" ></td>
<input type="HIDDEN" id="matricule" name="matricule[]" size="6" maxlength="6" value="'.$data_voyageur["matricule"].'" >
<td><select style="width:110px;" id="civilite" name="civilite[]" >
<option selected="selected">'.$data_voyageur["civilite"].'
<option value="Monsieur" >Monsieur</option >
<option value="Madame">Madame</option >
</select></td>
<td><input type="text" id="nom" name="nom[]" size="20" maxlength="255" value="'.$data_voyageur["nom"].'" ></td>
<td><input type="text" id="nom_j" name="nom_j[]" size="20" maxlength="255" value="'.$data_voyageur["nom_j"].'" ></td>
<td><input type="text" id="prenom" name="prenom[]" size="25" maxlength="255" value="'.$data_voyageur["prenom"] .'" ></td>
<td><input type="date" id="date_n" name="date_n[]" size="8" maxlength="15" value="'.$data_voyageur["date_n"].'" ></td>
<td><select id="type_chambre" name="type_chambre[]" >
<option selected="selected"> </option>';
?>
<?php include("liste/type_chambre.php"); ?>
<?php
echo' <td><select id="num_chambre" name="num_chambre[]" >
<option selected="selected"> </option>';
?>
<?php include("liste/num_chambre.php"); ?>
<?php
echo' <td><select id="montant_t" name="montant_t[]" >';
echo '<option selected="selected"></option>';
$param_liste_tranche = $bd->query('SELECT id_voyage, id_ligne, tranche, montant_t
FROM param_sous_voyage WHERE id_voyage ="' . $data_voyage['id']. '" ORDER BY tranche ASC ');
while ($lise_Tranche = $param_liste_tranche->fetch())
{
echo '<option value="'.$lise_Tranche["montant_t"].'">'. $lise_Tranche["montant_t"].'</option>';
}
echo '
<td><select id="information" name="information[]" >
<option selected="selected">Aucune</option>
<option value="SANS PORC">SANS PORC</option>
<option value="SANS SEL">SANS SEL</option>
</select></td>
<td><input type="text" id="nationalite" name="nationalite[]" size="25" maxlength="15" value="'.$data_voyageur["nationalite"].'" ></td>
<td><input type="text" id="sexe" name="sexe[]" size="1" maxlength="3" value=""></td>
<td><select id="piece" name="piece" >
<option selected="selected">'.$data_voyage["piece"].'</option>
<option value="PASSEPORT">PASSEPORT</option >
<option value="CNI">CNI</option >
</select></td>
</tr></tbody></table>
';
}
}
else {
echo "<H3>En attente de sélection d'un profil</H3>";
}
?>
<input type="submit" value="OK">
</form>
Ensuite la page de validation
<?php
$req = $bd->prepare('INSERT INTO voyage SET
id_perso = :id_perso,
id_ligne_voyage = :id_ligne_voyage,
destination= :destination,
type= :type,
matricule= :matricule,
civilite= :civilite,
nom= :nom,
nom_j= :nom_j,
prenom= :prenom,
date_n= STR_TO_DATE(:date_n,"%d/%m/%Y"),
type_chambre=:type_chambre,
num_chambre=:num_chambre,
tarif=:tarif,
information=:information,
nationalite=:nationalite,
sexe=:sexe,
piece=:piece
');
foreach($_POST['id_perso'] as $i => $v) {
$req->execute(array(
':id_perso'=>$v,
// pour le reste faut utiliser l'index ;)
':id_ligne_voyage'=>$_POST['id_ligne_voyage'],
':destination'=>$_POST['destination'],
':type'=>$_POST['type'],
':matricule'=>$_POST['matricule'],
':civilite'=>$_POST['civilite'],
':nom'=>$_POST['nom'],
':nom_j'=>$_POST['nom_j'],
':prenom'=>$_POST['prenom'],
':date_n'=>$_POST['date_n'],
':type_chambre'=>$_POST['type_chambre'],
':num_chambre'=>$_POST['num_chambre'],
':tarif'=>$_POST['montant_t'],
':information'=>$_POST['information'],
':nationalite'=>$_POST['nationalite'],
':sexe'=>$_POST['sexe'],
':piece'=>$_POST['piece']
));
}
?>