L3 Info : PHP et Applications Web
 
◃  Ch. 4 Le protocole HTTP  ▹
 

Illustrations des incohérences produites :

Cas 1 : édition et méthode GET

  • On dispose d'une table Nom(int id, string nom) avec une gestion automatisée de la clé.
  • On réalise un formulaire pour mémoriser de nouveaux noms dans la table qui utilise la méthode GET
  • Résultat : script d'insertion avec GET

Cas 2 : passage à la méthode POST

Code PHP (version GET)

try {
    if (isset($_GET['action'])) {
        insererNom($_GET['nom']; // fonction réalisant l'INSERT SQL
    }
    else {
        initTableNom(); // fonction qui : 
                          // 1. supprime la table Nom, 
                          // 2. recrée la table Nom, 
                          // 3. insère 3 enregistrements
    }

    $tab = getAllNoms(); // fonction qui :
                  // 1. execute "SELECT * FROM nom"
                  // 2. récupère le résultat dans un tableaux de tableaux associatifs

    echo "<h1>Contenu de la table nom</h1>";
    echo "<table border='1'>\n";
    $colnames = array_keys($tab[0]);
    echo "<tr>\n";
    foreach ($colnames as $col) {
        echo "<th>" . $col . "</th>";
    }
    echo "</tr>\n";

    foreach ($tab as $ligne) {
        echo "<tr>\n";
        foreach ($ligne as $col) {
            echo "<td>" . $col . "</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
} catch (Exception $e) {
    print "<br />Erreur !: " . $e->getMessage() . "<br/>";
    die();
}
echo "<h2>Formulaire utilisant la méthode GET</h2>";
echo "<form action='' method=get>
<input type='text' name='nom' />
<input type='submit' name='action' value='inserer'/>
</form>";