IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Requete Insert multiple


Sujet :

PHP & Base de données

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut Requete Insert multiple
    Bonjour, j'ai un problème avec ma requête insert car je génère un tableau dynamique et je sais pas comment enregistrer les données de ma base pour chaque ligne du tableau.

    tableau:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
          </table>
              <?php
      $id_plan = $_POST['SIG_PLAN'];
    	$orders = $DB->query("SELECT id_wms FROM tbl_orders WHERE id_plan = '$id_plan'");
    ?>
     
    <?php
    	foreach ($orders as $order):
    ?>
           <table border="0" cellpadding="4" cellspacing="0" class="ligne_lot">
           <td><input value="<?= $order->id_wms ?>"  class="" type="text" name="SIG_PALCHARG[]"  id="SIG_ORDER" style="border: hidden;  width: 50px" readonly="readonly"/></td>
           <td><label><input value=""  class="" type="text" name="SIG_PALCHARG[]"  id="SIG_PALCHARG" style="border: hidden;  width: 50px"/></label></td>
           <td><label><input value=""  class="" type="text" name="SIG_PALCHEP[]"  id="SIG_PALCHEP" style="border: hidden; width: 50px"/></label></td>
           <td><label><input value=""  class="" type="text" name="SIG_PALBOIS[]"  id="SIG_PALBOIS" style="border: hidden; width: 50px"/></label></td>
     
     
     
    		</table>
    requete:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <?php
     
     $SIG_PLAN = $_POST['SIG_PLAN'];
     
    /* VALIDATE HOW YOU NEED TO VALIDATE */
    			if($_POST)
    				{
                   /* VALUES */
    	$SIG_ORDER = $_POST['SIG_PALCHARG'][];
    	$SIG_PALCHARG = $_POST['SIG_PALCHARG'][];
      $SIG_PALCHEP = $_POST['SIG_PALCHEP'][];  
    	$SIG_PALBOIS = $_POST['SIG_PALBOIS'][];
     
     
             $req = $DB->insert_update_delete("INSERT INTO log_sigboldep (SIG_PLAN, SIG_ORDER, SIG_PALCHARG, SIG_PALCHEP, SIG_PALBOIS) VALUES('$SIG_PLAN', '$SIG_ORDER', '$SIG_PALCHARG', '$SIG_PALCHEP', '$SIG_PALBOIS')");
    				} else { 
     
    						header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
    						exit();
    				}
     
    ?>

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Fait une boucle foreach sur tes résultats de formulaire.

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    je fais comment car je debute

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    foreach ($_POST['SIG_ORDER'] as $key=>$SIG_ORDER) {
        	$SIG_PALCHARG = $_POST['SIG_PALCHARG'][$key];
        	$SIG_PALCHEP = $_POST['SIG_PALCHEP'][$key];  
    	$SIG_PALBOIS = $_POST['SIG_PALBOIS'][$key];
     
             $req = $DB->insert_update_delete("INSERT INTO log_sigboldep (SIG_PLAN, SIG_ORDER, SIG_PALCHARG, SIG_PALCHEP, SIG_PALBOIS) VALUES('$SIG_PLAN', '$SIG_ORDER', '$SIG_PALCHARG', '$SIG_PALCHEP', '$SIG_PALBOIS')");
     
    }
    Il manque par contre la protection contre les injections SQL.
    Dans une répétition de requête, il est de plus interessant d'utiliser des requêtes préparées.

  5. #5
    Expert confirmé Avatar de papajoker
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2013
    Messages
    2 217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2013
    Messages : 2 217
    Points : 4 699
    Points
    4 699
    Par défaut
    dans le form il faut
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    <input value="<?php echo $_POST['SIG_PLAN']; ?>" name="SIG_PLAN"  type="hidden" >

  6. #6
    Expert éminent sénior

    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2010
    Messages
    5 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 5 389
    Points : 10 422
    Points
    10 422
    Par défaut
    Citation Envoyé par sabotage Voir le message
    Il manque par contre la protection contre les injections SQL.
    Dans une répétition de requête, il est de plus intéressant d'utiliser des requêtes préparées.
    Prends bien en compte ces remarques, car cela pourrait aussi fonctionner sans cela, mais c'est le moment propice pour t'intéresser aux requêtes préparées.

  7. #7
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    Ok merci mais ces erreurs maintenant:
    - Notice: Undefined index: SIG_ORDER in C:\wamp\www\formValidator\Submit_Check-out.php on line 8

    - Warning: Invalid argument supplied for foreach() in C:\wamp\www\formValidator\Submit_Check-out.php on line 8

  8. #8
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Tu as mis deux fois SIG_PALCHARG dans ton formulaire, j'ai supposé qu'un des deux devait être corrigé en SIG_ORDER.

  9. #9
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    D'accord j'avais corrige mais au final ma requête ressemble a quoi car je suis perdu la

  10. #10
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    parce que j'ai ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?php
            $SIG_PLAN = $_POST['SIG_PLAN'];
     
    /* VALIDATE HOW YOU NEED TO VALIDATE */
    			if($_POST)
    				{
                   /* VALUES */
    	$SIG_ORDER = $_POST['SIG_ORDER'];
    	$SIG_PALCHARG = $_POST['SIG_PALCHARG'];
      $SIG_PALCHEP = $_POST['SIG_PALCHEP'];  
    	$SIG_PALBOIS = $_POST['SIG_PALBOIS'];
     
     
     
    foreach ($_POST['SIG_ORDER'] as $key=>$SIG_ORDER) {
        	$SIG_PALCHARG = $_POST['SIG_PALCHARG'][$key];
        	$SIG_PALCHEP = $_POST['SIG_PALCHEP'][$key];  
    	$SIG_PALBOIS = $_POST['SIG_PALBOIS'][$key];
     
             $req = $DB->insert_update_delete("INSERT INTO log_sigboldep (SIG_PLAN, SIG_ORDER, SIG_PALCHARG, SIG_PALCHEP, SIG_PALBOIS) VALUES('$SIG_PLAN', '$SIG_ORDER', '$SIG_PALCHARG', '$SIG_PALCHEP', '$SIG_PALBOIS')");
     
    				}
    ?>

  11. #11
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Si tu présentais proprement ton code tu t'y retrouverais.
    Par exemple pourquoi ton $SIG_PLAN est avant le commentaire et la condition alors que les autres variables sont après ?

    Le code d'insertion devient donc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    foreach ($_POST['SIG_ORDER'] as $key=>$SIG_ORDER) {
        	$SIG_PALCHARG = $_POST['SIG_PALCHARG'][$key];
        	$SIG_PALCHEP = $_POST['SIG_PALCHEP'][$key];  
    	$SIG_PALBOIS = $_POST['SIG_PALBOIS'][$key];
    	$SIG_PLAN = $_POST['SIG_PLAN'][$key];
     
             $req = $DB->insert_update_delete("INSERT INTO log_sigboldep (SIG_PLAN, SIG_ORDER, SIG_PALCHARG, SIG_PALCHEP, SIG_PALBOIS) VALUES('$SIG_PLAN', '$SIG_ORDER', '$SIG_PALCHARG', '$SIG_PALCHEP', '$SIG_PALBOIS')");
     
    }
    ce que tu as mis avant est evidemment inutile.

    Je rappelle que ce code ne fait rien contre les injections SQL.

  12. #12
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    d'accord et je peux faire comment pour que je puisse avoir une requête préparé ?

  13. #13
    Expert confirmé Avatar de papajoker
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2013
    Messages
    2 217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2013
    Messages : 2 217
    Points : 4 699
    Points
    4 699
    Par défaut
    Si ton $DB est bien un objet pdo

    tu as un exemple ici : http://fmaz.developpez.com/tutoriels...dre-pdo/#LIV.a

  14. #14
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Octobre 2013
    Messages : 7
    Points : 1
    Points
    1
    Par défaut
    D'accord je vais regarder et j'ai toujours une erreur:
    Notice: Undefined index: '$order' in C:\wamp\www\formValidator\Submit_test.php on line 7
    Notice: Undefined index: '$order' in C:\wamp\www\formValidator\Submit_test.php on line 8
    Notice: Undefined index: '$order' in C:\wamp\www\formValidator\Submit_test.php on line 9
    Warning: Illegal string offset ''$order'' in C:\wamp\www\formValidator\Submit_test.php on line 10
    J'ai mis ma requête sur une page de test pour voir ce que ne va pas, savez-vous pourquoi j'ai ces erreur car je comprend pas pourquoi il me dit que $order est indefini

  15. #15
    Expert confirmé Avatar de papajoker
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2013
    Messages
    2 217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2013
    Messages : 2 217
    Points : 4 699
    Points
    4 699
    Par défaut
    comme julp je ne comprend pas
    si order est indéfini c'est que la requete est mauvaise

  16. #16
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    Je penche plutôt pour une variable entre quotes simples et donc non interprétée... Mais sans le code des lignes en erreur, difficile à dire

  17. #17
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <?php
      $id_plan = $_POST['SIG_PLAN'];
    	$orders = $DB->query("SELECT id_wms FROM tbl_orders WHERE id_plan = '$id_plan'");
    ?>
     
    <?php
    	foreach ($orders as $order):
    ?>
    Il n'en manquerait pas un bout ?
    Un $DB->fetchAll(); par exemple...

    => Comprendre PDO

Discussions similaires

  1. Requete Insert Multiple
    Par tonton93 dans le forum MS SQL Server
    Réponses: 6
    Dernier message: 22/03/2006, 11h36
  2. INSERT multiples avec : rs.AddNew et .Update
    Par M.Zip dans le forum ASP
    Réponses: 4
    Dernier message: 03/12/2004, 15h53
  3. Erreur lors d'une requete insert into.. select
    Par Mr N. dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 04/11/2004, 17h32
  4. effectuer une requete insert avec 'values' ET 'select'
    Par delaio dans le forum Bases de données
    Réponses: 4
    Dernier message: 15/08/2004, 19h05
  5. pb d'insertions multiples
    Par devalender dans le forum SQL
    Réponses: 2
    Dernier message: 14/07/2004, 14h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo