| id | nom | prix | proprio |
|---|---|---|---|
| 1 | 206 | 200 | Toto |
| 2 | 605 | 200 | Tata |
| 3 | VTV | 132154 | Jules |
| 4 | VTT | 42 | Plop |
| 5 | TGV | 23335 | Buzz l'éclair |
| 6 | Millenium Falcon | 53 | Babar |
| 7 | 21 | ||
| 8 | test | 21 | Bozzo le clown |
| 9 | moi | 45 | guillaume |
| 10 | Lada | 2 | URSS |
| 11 | idiot | 0 | guillaume qui est desole q'etre aussi bete... |
| 12 | esai | 45 | ech')"; DROP table test; // |
| 13 | oh que ca marche bien les addslashes | 1000 | guillaume |
| 14 | test | 0 | test |
<?php
2 /*
3 Voir l'exemple d'avant !!!
4 */
5
6 /* Cette fonction permet d'éviter des problèmes de sécurité lorsqu'on récupère des variables
7 via $_GET ou $_POST */
8 function protection_chaine($str) {
9 if(!get_magic_quotes_gpc()) {
10 $str = addslashes($str);
11 }
12
13 $str = htmlspecialchars($str);
14 return $str;
15 }
16
17 mysql_connect("localhost","ikfac","password"); /* Connection */
18 mysql_select_db("ikfac"); /* Choix de la base */
19
20 $nom = isset($_POST['nom']) ? $_POST['nom'] : "";
21 $prix = isset($_POST['prix']) ? $_POST['prix'] : "";
22 $proprio = isset($_POST['proprio']) ? $_POST['proprio'] : "";
23
24 if($nom && $prix && $proprio) { // si on a toutes les infos pour ajouter qq chose, on l'ajoute
25 $nom = protection_chaine($nom); //on protège la chaine, sinon les méchants pas beaux peuvent casser la base
26 $prix = (int) $prix; // prix est forcement un entier
27 $proprio = protection_chaine($proprio);
28 $sql="INSERT INTO test VALUES ('','$nom','$prix','$proprio')";
29 mysql_query($sql);
30 echo "1 ligne ajoutée ! ";
31 }
32
33 $by = isset($_GET['by']) ? $_GET['by'] : "";
34 $order = isset($_GET['order']) ? $_GET['order'] : "ASC";
35
36 echo "<h1>Création d'un tableau html</h1>";
37 echo "Cliquez sur les titres des colonnes";
38 $sql = "SELECT * FROM test"; /* On selectionne tout dans la table test */
39
40 if($by == "id") {
41 $sql .= " ORDER BY id ";
42 } else if($by == "nom") {
43 $sql .=" ORDER BY nom ";
44 } else if($by == "prix") {
45 $sql .=" ORDER BY prix ";
46 } else if($by == "proprio") {
47 $sql .=" ORDER BY proprio ";
48 } else {
49 $by = ""; /* by n'a pas une valeur valide -> on dit qu'il est égal a rien */
50 }
51
52 if($by && $order) { /* si on sais par quoi on peux trier */
53 if($order == "ASC") {
54 $sql .= " ASC ";
55 /* On inverse */
56 $order = "DESC";
57 } else {
58 $sql .= "DESC";
59 $order = "ASC";
60 }
61 }
62
63 $req = mysql_query($sql); /* On execute la requette */
64 ?>
65 <table style="border: 1px solid #000">
66 <tr>
67 <th><a href="exemple11.php?by=id&order=<? echo $order ?>">id</a></th>
68 <th><a href="exemple11.php?by=nom&order=<? echo $order ?>">nom</a></th>
69 <th><a href="exemple11.php?by=prix&order=<? echo $order ?>">prix</a></th>
70 <th><a href="exemple11.php?by=proprio&order=<? echo $order ?>">proprio</a></th>
71 </tr>
72 <?php
73 while($tab = mysql_fetch_array($req)) {
74 echo "<tr>";
75 echo "<td>". $tab['id'] ."</td>";
76 echo "<td>" . $tab['nom'] ."</td>";
77 echo "<td> " . $tab['prix'] . "</td>";
78 echo "<td>" . $tab['proprio'] . "</td>";
79 echo "</tr>";
80 }
81 ?>
82 </table>
83
84 <hr/>
85 <form method="post" action="exemple11.php">
86 <label for="nom">Nom: </label>
87 <input type="text" id="nom" name="nom" />
88 <label for="prix">Prix: </label>
89 <input type="text" id="prix" name="prix" />
90 <label for="proprio">Proprio: </label>
91 <input type="text" id="proprio" name="proprio" />
92 <input type="submit" value="Ajouter" />
93 </form>
94
95 <?php
96 mysql_close(); /* Fermeture de la connec */
97 /* On s'en fout là, c'est juste pour afficher la source */
98 include("hl.php");
99 hl('exemple11.php');
100 ?>