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 :

$_POST, array et insert mysql. [MySQL]


Sujet :

PHP & Base de données

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Conseil

    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 54
    Points
    54
    Par défaut $_POST, array et insert mysql.
    Bonjour à tous, j'ai un tableau que je récupère via une requete php mysql (en fait je récupère plusieurs enregistrements d'une table) et le tout s'affiche ligne par ligne dans des champs html de type texte. voici la requête:

    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
    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
    <?php
     
    $sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
    $qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
     
    $get_date = function($data_jours) {
       $today = new DateTime();
       $today->setTime(0, 0);
       $jours = (isset($_POST['jours']) && ctype_digit($_POST['jours'])) ? $_POST['jours'] : 0;
       if (isset($_POST['submit']) && ($_POST['submit'] === 'moins')) {
          $jours = -$jours;
       }
       $nb       = $data_jours + $jours;
       $func     = ($nb < 0) ? 'sub' : 'add';
       $interval = new DateInterval("P{$nb}D");
       return $today->$func($interval)->format('d-m-Y');
    };
    $i = -1; // index des enregistrements
    ?>
    <table cellpadding="5" cellspacing="5">
       <tr>
          <td><strong>CODE SCENARIO</strong></td>
          <td><strong>LIBELLE</strong></td>
          <td><strong>ACTION</strong></td>
          <td><strong>DESCRIPTION</strong></td>
          <td><strong>DATE</strong></td>
       </tr>
       <form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
          <input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
          <?php while($row = mysql_fetch_assoc($qry)): ?>
          <tr>
             <td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
             <td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
             <td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
             <td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
             <td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']); ?>" size="12"></td>
          </tr>
          <?php endwhile; ?>
          <p>
             <strong>Décalage des date </strong>
             <table width="150" border="0" cellspacing="0" cellpadding="0">
                <tr>
                   <td align="center" bgcolor="#FFFF99"><input type="hidden" name="code_s" value="<?php echo $_POST['code_s']; ?>">
                      <input type="image" src="images/minus_remove_green.png" width="22" height="22" name="submit"  value="moins" />
                      <input name="jours" type="text" value="" size="5" />
                      <input type="image" src="images/plus_add_green.png" width="22" height="22" name="submit" value="plus" />
                   </td>
                </tr>
             </table>
    ensuite je fais un form method post sur une autre page et la j'essaye d'insérer les données récupérées et modifiées via les input, j'ai donc fais 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
    <?php
    //var_dump($_POST['data']) ;
    $value = $_POST['data'] ; 
     
    foreach($value as $cle => $valeur)
    {   
     
    $sql = 'INSERT INTO agenda SET
    code_s = "'.mysql_real_escape_string($_POST['data[][code_s]']).'",
    date = "'.mysql_real_escape_string($_POST['data[][date]']).'", 
    libelle = "'.mysql_real_escape_string($_POST['data[][libelle]']).'",
    action = "'.mysql_real_escape_string($_POST['data[][libelle]']).'"';
     
    }
    ?>
    le soucis c'est que j'ai plein de message d'erreur, a priori je fais quelque chose de mauvais mais quoi? je ne sais pas.


    voila ce que j('ai à titre indicatif:
    ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    # Time Memory Function Location
    1 0.0006 727504 {main}( ) ..\go.php:0

    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    # Time Memory Function Location
    1 0.0006 727504 {main}( ) ..\go.php:0

    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    # Time Memory Function Location
    1 0.0006 727504 {main}( ) ..\go.php:0

    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    # Time Memory Function Location
    1 0.0006 727504 {main}( ) ..\go.php:0

    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    # Time Memory Function Location
    1 0.0006 727504 {main}( ) ..\go.php:0

    D'avance merci pour votre aide.

  2. #2
    Membre expérimenté
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2008
    Messages
    1 051
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hautes Pyrénées (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 051
    Points : 1 638
    Points
    1 638
    Par défaut
    Je ne vois pas d'où vient ton $_POST['data'].

    Cependant tu n'accèderas pas à ta donnée comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $_POST['data[][code_s]']
    Je doute que ton POST te renvoie un tableau l'ami ...
    Règle N° 1 : Si tout va bien, ne touchez à rien.

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Conseil

    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 54
    Points
    54
    Par défaut
    mais je ne sais pas comment procéder alors,
    car quand je vais cela:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <?php var_dump($_POST['data']) ; ?>
    il me renvoit bien :

    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
    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
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    array
      0 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      1 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      2 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      3 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      4 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      5 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'PAIEMENT' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
      6 => 
        array
          'code_s' => string 'PM' (length=2)
          'titre' => string 'PAIEMENT CHQ' (length=12)
          'action' => string 'POSITION' (length=8)
          'libelle' => string '' (length=0)
          'date' => string '09-05-2012' (length=10)
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][code_s] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 190
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][date] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 191
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 192
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0
     
    ( ! ) Notice: Undefined index: data[][libelle] in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\go.php on line 193
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0008	705424	{main}( )	..\go.php:0

  4. #4
    Membre expérimenté
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2008
    Messages
    1 051
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hautes Pyrénées (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 051
    Points : 1 638
    Points
    1 638
    Par défaut
    Le problème c'est que tu fais une boucle sur chaque clé, mais tu t'en sers pas dans ta boucle

    Essai d'y accéder comme ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    for($i=0;$i<count($value);$i++)
    {
       $sql = 'INSERT INTO agenda SET
    code_s = "'.mysql_real_escape_string($value[$i]['code_s']).'",
    date = "'.mysql_real_escape_string($value[$i]['date']).'", 
    libelle = "'.mysql_real_escape_string($value[$i]['libelle']).'",
    action = "'.mysql_real_escape_string($value[$i]['libelle']).'"'; // Tu insères deux fois le libelle ?
    }
    Règle N° 1 : Si tout va bien, ne touchez à rien.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 24/05/2015, 15h31
  2. [MySQL] Array et insert dans mysql
    Par yamatoshi dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 02/12/2009, 18h54
  3. [MySQL] Récupérer $_POST et insertion MySql
    Par hugo69 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 03/12/2006, 20h23
  4. Problème sur requête insert mysql
    Par kcizth dans le forum Requêtes
    Réponses: 5
    Dernier message: 04/02/2006, 18h37
  5. Array en requête mysql
    Par Anduriel dans le forum Requêtes
    Réponses: 1
    Dernier message: 10/10/2005, 17h32

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