Création d'un tableau html

Cliquez sur les titres des colonnes
id nom prix proprio
1206 200Toto
2605 200Tata
3VTV 132154Jules
4VTT 42Plop
5TGV 23335Buzz l'éclair
6Millenium Falcon 53Babar
7 21
8test 21Bozzo le clown
9moi 45guillaume
10Lada 2URSS
11idiot 0guillaume qui est desole q'etre aussi bete...
12esai 45ech')"; DROP table test; //
13oh que ca marche bien les addslashes 1000guillaume
14test 0test


Code

1 <?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&amp;order=<? echo $order ?>">id</a></th>
68         <th><a href="exemple11.php?by=nom&amp;order=<? echo $order ?>">nom</a></th>
69         <th><a href="exemple11.php?by=prix&amp;order=<? echo $order ?>">prix</a></th>
70         <th><a href="exemple11.php?by=proprio&amp;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
?>