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

Langage PHP Discussion :

Afficher un tableau depuis une base de donnée


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2016
    Messages
    275
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2016
    Messages : 275
    Points : 76
    Points
    76
    Par défaut Afficher un tableau depuis une base de donnée
    j'ai réaliser un code en JS mais j'ai une erreur
    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
    <?php
     
    // Replace these parameters to match your database
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'Mm101010';
    $dbname = 'smartphone';
    $table  = 'vu_affet_empl';
     
    // Connect
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
     
    // If parameters are received
    if(isset($_POST['x']) && isset($_POST['y'])){
     
        $error_messages = array();
     
        if ($mysqli->connect_errno) {
            $error_messages[] = "Couldn't connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }
     
        // Prepare insert
        if (!($stmt = $mysqli->prepare("INSERT INTO `$table` (x,y) VALUES (?,?)"))) {
            $error_messages[] = "Couldn't prepare statement: (" . $mysqli->errno . ") " . $mysqli->error;
        }
     
        // Bind parameters x and y
        if (!$stmt->bind_param("dd", $_POST['x'], $_POST['y'])) {
            $error_messages[] = "Couldn't bind parameters: (" . $stmt->errno . ") " . $stmt->error;
        }
     
        // Execute the insert
        if (!$stmt->execute()) {
            $error_messages[] = "Couldn't execute the query: (" . $stmt->errno . ") " . $stmt->error;
        }
     
        // Prepare some data to return to the client (browser)
        $result = array(
            'success' => count($error_messages) == 0,
            'messages' => $error_messages
        );
     
        $stmt->close();
     
        // Send it
        echo json_encode($result);
        // Exit (do not execute the code below this line)
        exit();
     
    } // end if
     
     
    // Fetch all the coordinates to display them in the page
    $res = $mysqli->query("SELECT * FROM `$table`");
    $rows = array();
    while ($row = $res->fetch_array()) {
        $rows[] = $row;
    }
     
    $mysqli->close();
    ?>
     
     
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Test page</title>
        <style>
        table{ border-collapse: collapse; }
        th,td{ border: 1px solid #888; padding: .3em 2em; }
        </style>
    </head>
    <body>
        <h1>New coordinates</h1>
        <table id="coordinates">
            <tr>
                    <td>&nbsp;</td>
                    <th>USER ID</th>
                    <th>Nom</th>
                    <th>Prenom</th>
                    <th>Num SIM</th>
                    <th>PIN Terminal</th>
                    <th>PIN SIM</th>
                    <th>Num EMEI</th>
                    <th>Date Debut</th>
                    <th>Date Fin</th>
                    <th>Vitre</th>
                    <th>Coque</th>
                    <th>Support Vehicule</th>
                    <th>Actif</th>
                    <th>Or Affectation1</th>
                    <th>Statut</th>
            </tr>
    <?php
     
    if(count($rows)){
        foreach($rows as $row){
    ?>
            <tr>
                    <td><input type="radio" name="select" class="del_customer" value="<?php echo $row["USER_ID"]; ?>" /></td>
                    <td><? $row["USER_ID"]?></td>
                    <td><? $row["Nom"]?></td>
                    <td><? $row["Prenom"]?></td>
                    <td><? $row["Num_SIM"]?></td>
                    <td><? $row["PIN_Terminal"]?></td>
                    <td><? $row["PIN_SIM"]?></td>
                    <td><? $row["Num_IMEI"]?></td>
                    <td><? $row["Date_Debut"]?></td>
                    <td><? $row["Date_Fin"]?></td>
                    <td><? $row["Vitre"]?></td>
                    <td><? $row["Coque"]?></td>
                    <td><? $row["Support_Vehicule"]?></td>
                    <td><? $row["Actif"]?></td>
                    <td><? $row["Or_Affectation1"]?></td>
                    <td><? $row["Statut"]?></td>
            </tr>
    <?php
        }
    }
     
    ?>
        </table>
     
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script>
            $(function(){
                $('#addBtn').click(postCoordinates);
     
                function postCoordinates(){
                    var USER_ID = $('#USER_ID').val(),
                        Nom = $('#Nom').val(),
                        Prenom = $('#Prenom').val(),
                        Num_SIM = $('#Num_SIM').val(),
                        PIN_Terminal = $('PIN_Terminal').val(),
                        PIN_SIM = $('#PIN_SIM').val(),
                        Num_IMEI = $('#Num_IMEI').val(),
                        Date_Debut = $('#Date_Debut').val(),
                        Date_Fin = $('#Date_Fin').val(),
                        Vitre = $('#Vitre').val(),
                        Coque = $('#Coque').val(),
                        Support_Vehicule = $('#Support_Vehicule').val(),
                        Actif = $('#Actif').val(),
                        Or_Affectation1 = $('#Or_Affectation1').val(),
                        Statut = $('#Statut').val();
     
                    if(x.length && y.length){
                        // Change this if it is on a different page
                        var url = window.location.href;
                        $.ajax({
                            'url': url,
                            'method': 'post',
                            'data': {'USER_ID': USER_ID, 'Nom': Nom, 'Prenom': Prenom, 'Num_SIM': Num_SIM, 'PIN_Terminal': PIN_Terminal, 'PIN_SIM': PIN_SIM, 'Num_IMEI': Num_IMEI, 'Date_Debut': Date_Debut, 'Date_Fin': Date_Fin, 'Vitre': Vitre, 'Coque': Coque, 'Support_Vehicule': Support_Vehicule, 'Actif': Actif, 'Or_Affectation1': Or_Affectation1, 'Statut': Statut},
                            'dataType': 'json', // The server will return json
                            'success': function(res){
                                if(res.hasOwnProperty('success') && res.success){
                                    $('#USER_ID, #Nom, #Prenom, #Num_SIM, #PIN_Terminal, #PIN_SIM, #Num_IMEI, #Date_Debut, #Date_Fin, #Vitre, #Coque, #Support_Vehicule, #Actif, #Or_Affectation1, #Statut').val(''); // Empty the inputs
                                    $('#coordinates').append('<tr><td>'+USER_ID+'</td><td>'+Nom+'</td></tr><tr><td>'+Num_SIM+'</td><td>'+PIN_Terminal+'</td></tr><tr><td>'+PIN_SIM+'</td><td>'+Num_IMEI+'</td></tr><tr><td>'+Date_Debut+'</td><td>'+Date_Fin+'</td></tr><tr><td>'+Vitre+'</td><td>'+Coque+'</td></tr><tr><td>'+Support_Vehicule+'</td><td>'+Actif+'</td></tr><tr><td>'+Or_Affectation1+'</td><td>'+Statut+'</td></tr>');
                                } else if(res.hasOwnProperty('messages')) {
                                    alert( res.messages.join('\n') );
                                }
                            },
                            'error': function(x, s, e){
                                alert( 'Error\n' + s + '\n' + e );
                            }
                        });
                    } else {
                        alert('You need to provide x and y coordinates');
                    }
                }
            });
        </script>
     
    </body>
    </html>
    Nom : Capture5NG.PNG
Affichages : 193
Taille : 13,4 Ko
    je voudrais affiché un tableau depuis une base de donnée
    merci

  2. #2
    Expert éminent
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Urbaniste
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    Points : 9 127
    Points
    9 127
    Par défaut
    ça s'appelle une erreur php
    donc forum php

    et encore une fois c'est une mauvaise pratique que de mélanger php et js
    A+JYT

  3. #3
    Membre émérite
    Avatar de badaze
    Homme Profil pro
    Chef de projets info
    Inscrit en
    Septembre 2002
    Messages
    1 412
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets info
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2002
    Messages : 1 412
    Points : 2 522
    Points
    2 522
    Par défaut
    @vinkey_33

    Regarde s'il n'y a pas erreur SQL à l'exécution du query.
    Cela ne sert à rien d'optimiser quelque chose qui ne fonctionne pas.

    Mon site : www.emmella.fr

    Je recherche le manuel de l'Olivetti Logos 80B.

Discussions similaires

  1. Réponses: 2
    Dernier message: 13/05/2016, 20h23
  2. remplir un tableau à deux dimensions depuis une base de donnée
    Par abbescr7 dans le forum Collection et Stream
    Réponses: 8
    Dernier message: 13/05/2016, 14h25
  3. Réponses: 1
    Dernier message: 17/03/2010, 15h33
  4. [SQL] Afficher une image depuis une base de données
    Par shenz dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 16/01/2008, 00h46
  5. Réponses: 8
    Dernier message: 29/09/2006, 12h08

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