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 :

Récupération variable avec <select>


Sujet :

PHP & Base de données

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Récupération variable avec <select>
    Bonjour à vous tous,

    Voilà j'ai un soucis pour récupérer variable de plusieurs select distincts.

    En base j'ai les tables suivantes :

    fiches_infos (id, titre, description, paragraphe1, id_cat, id_niveau, id_auteur)
    fiches_cat (id_cat, categorie) : déjà remplie
    fiches_niveau (id_niveau, niveau) : déjà remplie
    fiches_auteur (id_auteur, nom, prenom, email, url)

    sachant que l'id de la fiche est unique, le contenu des tables est le suivant :
    categories
    1 : guitare
    2 : basse
    3 : batterie ... jusqu'à 7

    auteurs :
    1 : nom : prenom : email : url

    niveau
    1 : debutant
    2 : confirme
    3 : expert

    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
     
    <-- LE FORMULAIRE -->
    <?php   
    if (isset($_GET['modifier_fiche'])) // Si on demande de modifier une fiche
    {       
        // On protège la variable "modifier_fiche" pour éviter une faille SQL
        $_GET['modifier_fiche'] = mysql_real_escape_string(htmlspecialchars($_GET['modifier_fiche']));
        // On récupère les infos de la correspondante
        $retour = mysql_query('SELECT * FROM fiches_infos WHERE id=\'' . $_GET['modifier_fiche'] . '\'');
        $donnees = mysql_fetch_array($retour);
     
     
        // On place les elements du formulaire dans des variables simples
        $categorie = stripslashes($donnees['categorie']);
        $id_niveau = stripslashes($donnees['id_niveau']); 
        $niveau = stripslashes($donnees['niveau']);  
     
        $id_auteur= stripslashes($donnees['id_auteur']);
     
        $titre = stripslashes($donnees['titre']);
        $description = stripslashes($donnees['description']);
        $paragraphe1 = stripslashes($donnees['paragraphe1']);
     
        $id_cat = $donnees['id']; // Cette variable va servir pour se souvenir que c'est une modification
    }       
    else // C'est qu'on rédige une nouvelle fiche
    {       
        $categorie = '';
     
        $id_niveau = '';
        $niveau = '';
     
        $id_auteur = '';
     
        $titre = '';
        $description = '';
        $paragraphe1 = '';
     
        $id_cat = 0; // La variable vaut 0, donc on se souviendra que ce n'est pas une modification
    }
    // on ferme la connexion à mysql 
    ?>
     
    <form method="post" action="liste_fiche.php">
    <center>
     
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td colspan="3" align="left">--------------------------------INFORMATIONS GENERALES---------------------</td>
    </tr>
    <tr>
    <td width="150" height="30" align="right">Choisissez votre instrument</td>
    <td width="10"></td>
    <td>
     
     
     
    <?php
    echo '<select size=1 name="categorie">'."\n";
    $result = mysql_query("SELECT categorie FROM fiches_cat" );
    while($data =  mysql_fetch_array($result))
    {
    echo '<option value="'.$data[0].'">'.$data['categorie'];
    echo '</option>'."\n";
    }
    echo '</select>'."\n";
    ?>
     
    	<input type="hidden" name="id_cat" value="<?php echo $id_cat; ?>"></td>
    </tr>
    <tr>
    <td width="150" height="30" align="right">Choisissez votre niveau</td>
    <td width="10"></td>
    <td>
    <?php
    echo '<select size=1 name="niveau">'."\n";
    $result = mysql_query("SELECT niveau FROM fiches_niveau" );
    while($data =  mysql_fetch_array($result))
    {
    echo '<option value="'.$data[0].'">'.$data['niveau'];
    echo '</option>'."\n";
    }
    echo '</select>'."\n";
    ?>
    <input type="hidden" name="id_niveau" value="<?php echo $id_niveau; ?>"></td>
    </tr>
    <tr>
    <td width="150" height="30" align="right">Choisissez lauteur</td>
    <td width="10">&nbsp;</td>
    <td>
    <?php
    echo '<select size=1 name="nom">'."\n";
    $result = mysql_query("SELECT nom FROM fiches_auteur" );
    while($data =  mysql_fetch_array($result))
    {
    echo '<option value="'.$data[0].'">'.$data['nom'];
    echo '</option>'."\n";
    }
    echo '</select>'."\n";
    mysql_close();
    ?>
    <input type="hidden" name="id_auteur" value="<?php echo $id_auteur; ?>"></td>
    </tr>
    <tr>
    <td colspan="3" align="left">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="3" align="left">--------------------------------CONTENU DE LA FICHE---------------------</td>
    </tr>
    <tr>
    	<td width="150" height="30" align="right">titre</td>
    	<td width="10">&nbsp;</td>
    	<td align="left"><input type="text" name="titre" size="50" value="<?php echo $titre; ?>" maxlength="70"></td>
    </tr>
    <tr>
    	<td width="150" height="30" align="right">description</td>
    	<td width="10">&nbsp;</td>
    	<td align="left"><input type="text" name="description" size="50" value="<?php echo $description; ?>"></td>
    </tr>
    <tr>
    	<td colspan="3">&nbsp;</td>
    </tr>
    <tr>
    	<td width="150" height="30" align="right" valign="top">paragraphe1</td>
    	<td width="10">&nbsp;</td>
    	<td align="left"><textarea name="paragraphe1" rows="15" COLS="70"><?php echo $paragraphe1; ?></textarea></td>
    </tr>
    <tr>
    	<td>&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="3" align="center"><input type="submit" value="Envoyer" name="envoyer"></td>
    </tr>
    </table>
     
    </center>
    </form><br /><br />
    <div align="center">
    <a href="liste_fiche.php">Retour à la liste des fiches</a><br /><br />
    </div>
    </body>
    </html>

    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
     
    <-- page affichage -->
    <?php
    //-----------------------------------------------------
    // Vérification 1 : est-ce qu'on veut poster une fiche ?
    //-----------------------------------------------------
     
     
    if(isset($_POST['id_cat']))
    {
        $id_cat = addslashes($_POST['id_cat']);                                                               
        $categorie = addslashes($_POST['categorie']); 
     
        $id_niveau = addslashes($_POST['id_niveau']);     
        $niveau = addslashes($_POST['niveau']);                                                            
     
        $id_auteur= addslashes($_POST['id_auteur']);                  
     
        $titre = addslashes($_POST['titre']);               
        $description= addslashes($_POST['description']);    
        $paragraphe1 = addslashes($_POST['paragraphe1']);   
     
        if ($_POST['id_cat'] == 0)
        {
            // Ce n'est pas une modification, on crée une nouvelle entrée dans la table "fiches_infos"
             //mysql_query("INSERT INTO fiches_infos VALUES('', '" . time() . "', '" . $titre. "', '" . $description . "', '" . $paragraphe1 . "', '" . $id_cat . "', '" . $id_auteur . "', '" . $id_niveau . "')");
            mysql_query("INSERT INTO fiches_infos VALUES('', '" . $titre. "', '" . $description . "', '" . $paragraphe1 . "', '" . $id_cat . "', '" . $id_auteur . "', '" . $id_niveau . "')");
     
        }
        else
        {
            // On protège la variable "id_fiche" pour éviter une faille SQL
            $_POST['id_cat'] = addslashes($_POST['id_cat']);
            // C'est une modification, on met juste à jour
            mysql_query("UPDATE fiches_infos SET titre='" . $titre . "', description='" . $description . "', paragraphe1='" . $paragraphe1 . "', id_cat='" . $id_cat . "', id_auteur='" . $id_auteur . "', id_niveau='" . $id_niveau . "' WHERE id_cat='" . $_POST['id_cat'] . "'");
        	}
    }
    //--------------------------------------------------------
    // Vérification 2 : est-ce qu'on veut supprimer une fiche ?
    //--------------------------------------------------------
     
    if (isset($_GET['supprimer_fiche'])) // Si on demande de supprimer une fiche
    {
        // Alors on supprime le lien correspondant
        // On protège la variable "id_fiche" pour éviter une faille SQL
        $_GET['supprimer_fiche'] = addslashes($_GET['supprimer_fiche']);
        mysql_query('DELETE FROM fiches_infos WHERE id=\'' . $_GET['supprimer_fiche'] . '\'');
    }
    ?>
     
    <table cellpadding="1" cellspacing="1">
    <tr bgcolor="#DDDDDD">
    <th></th>
    <th></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=fiches_infos.id_cat" title="ranger">' ?>id_cat</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=fiches_infos.id_auteur" title="ranger">' ?>id_auteur</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=fiches_cat.categorie" title="ranger">' ?>categorie</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=fiches_niveau.niveau" title="ranger">' ?>niveau</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=fiches_auteur.nom" title="ranger">' ?>Auteur</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=titre" title="ranger">' ?>titre</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=description" title="ranger">' ?>description</a></span></th>
    <th height="20"><span class="texte_courant_gris_b"><?php echo '<a href="liste_fiche.php?rangerpar=paragraphe1" title="ranger">' ?>paraph1</a></span></th>
    </tr>
     
    <?php
    if(isset($_GET['rangerpar'])) $rangerpar = $_GET['rangerpar'];
    else $rangerpar = "id";
     
    $myrequetecheri = 'SELECT fiches_niveau.niveau, fiches_niveau.id_niveau, fiches_cat.categorie, fiches_cat.id_cat, fiches_infos.id_cat, fiches_infos.id_auteur, fiches_infos.id, fiches_infos.titre, fiches_infos.description, fiches_infos.paragraphe1, fiches_auteur.id_auteur, fiches_auteur.nom, fiches_auteur.prenom, fiches_auteur.email, fiches_auteur.url FROM fiches_infos, fiches_auteur, fiches_niveau, fiches_cat WHERE fiches_infos.id_auteur = fiches_auteur.id_auteur ORDER BY ' . $rangerpar;
    //$myrequetecheri = 'SELECT fiches_niveau.niveau, fiches_niveau.id_niveau, fiches_cat.categorie, fiches_cat.id_fiche, fiches_infos.id_fiche, fiches_infos.id_auteur, fiches_infos.id, fiches_infos.titre, fiches_infos.description, fiches_infos.paragraphe1, fiches_auteur.nom, fiches_auteur.prenom, fiches_auteur.email, fiches_auteur.url, fiches_auteur.nom FROM fiches_infos, fiches_auteur, fiches_niveau, fiches_cat WHERE fiches_infos.id_auteur = fiches_auteur.id_auteur AND fiches_infos.id_niveau = fiches_niveau.id_niveau AND fiches_infos.id_fiche = fiches_cat.id_fiche ORDER BY ' . $rangerpar;
     
    $retour = mysql_query($myrequetecheri);
     
     
    while ($donnees = mysql_fetch_array($retour)) // On fait une boucle pour lister les liens 
    {                                                                                         
    ?>
    <tr bgcolor="#FFFFFF">
    <th><?php echo '<a href="rediger_fiche.php?modifier_fiche=' . $donnees['id'] . '" title="modifier">'; ?><img src="img/picto_modifier.gif" border="0"></a></th>
    <th><?php echo '<a href="liste_fiche.php?supprimer_fiche=' . $donnees['id'] . '" title="supprimer">'; ?><img src="img/picto_supprimer.gif" border="0"></a></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['id_cat']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['id_auteur']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['categorie']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['niveau']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['nom']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['titre']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['description']); ?></span></th>
    <th><span class="texte_courant_gris"><?php echo stripslashes($donnees['paragraphe1']); ?></span></th>
    </tr>
     
    <?php
    }
    // on ferme la connexion à mysql 
    mysql_close();
    ?>
    CONCLUSION :
    Dans la page du formuliare, je recup bien les champs des différentes tables. Par contre quand je veux en créer une, l'id_cat est toujours zéro dans la table fiches_infos, et id_ateur, id_niveau, aucune valeur.

    Je tourne en rond depuis un petit bout de temps, merci à vous d'avance

  2. #2
    Membre actif Avatar de Momodedf
    Inscrit en
    Juillet 2007
    Messages
    246
    Détails du profil
    Informations personnelles :
    Âge : 35

    Informations forums :
    Inscription : Juillet 2007
    Messages : 246
    Points : 221
    Points
    221
    Par défaut
    Par contre quand je veux en créer une, l'id_cat est toujours zéro dans la table fiches_infos, et id_ateur, id_niveau, aucune valeur.
    Et alors ce n'est pas le but ?
    Vu ton code c'est normal :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $id_niveau = '';
    $id_auteur = '';
    $id_cat = 0;

Discussions similaires

  1. récupération variable avec plusieurs appels Javascript
    Par aikiox dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 23/04/2012, 17h38
  2. [MySQL] comment déclarer des valeurs de variables avec jointure SELECT Mysql
    Par monlou dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 21/07/2010, 00h17
  3. pb : VIEW avec variable dans le select
    Par seb.briet dans le forum SQL Procédural
    Réponses: 4
    Dernier message: 13/05/2006, 01h57
  4. [MySQL] Problème récupération de données avec un SELECT DISTINCT
    Par 12_darte_12 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 25/07/2005, 14h48
  5. [VB.NET] Problem de récupération de variable avec une DLL
    Par ludovic85 dans le forum Windows Forms
    Réponses: 11
    Dernier message: 19/01/2005, 11h37

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