DEL Old file.
[appart.git] / traitement_inputs.php
1 <?php
2
3 /**
4 * Renvoie la valeur "SQL" d'un champ.
5 * p1 : sa valeur
6 * p2 : son type
7 */
8 function getChampSQL($valeur, $champ, $type)
9 {
10 $champSQL = trim($valeur);
11 if ($champ == 'statut' && $champSQL == '')
12 $champSQL = '0';
13 if ($type == 'int' && ($champSQL == '' OR !is_numeric($champSQL)))
14 $champSQL = 'NULL';
15 if ($type == 'date' && $champSQL == '')
16 $champSQL = 'NULL';
17 if ($type == 'timestamp' && $champSQL == '')
18 $champSQL = 'NULL';
19
20 if ($champSQL != 'NULL')
21 $champSQL = "'".(str_replace("'", "''", $champSQL))."'";
22
23 return $champSQL;
24 }
25
26 $inputs = array_merge($_GET, $_POST);
27
28 $erreur = '';
29
30 // l'id de l'appart afficher, à modifier, à  ajouter ou à  supprimer
31 $id = 1;
32 if (array_key_exists('id', $inputs))
33 $id = $inputs['id'];
34
35 // l'action peut valoir :
36 // 'voir' pour voir un appartement
37 // 'mémoriser' pour modifier un appartement (ou ajouter)
38 // 'ajouter' pour ajouter un nouvel appart
39 // 'supprimer' pour supprimer un appart
40 $action = 'voir';
41 if (array_key_exists('action', $inputs))
42 $action = strtolower($inputs['action']);
43
44 if ($action == 'ajouter')
45 {
46 $result = pg_query("SELECT nextval('appart_id_seq')");
47 $appartData = pg_fetch_row($result);
48 $id = $appartData[0];
49 pg_free_result($result);
50 }
51 else if ($action == 'mémoriser')
52 {
53 $result = pg_query("SELECT count(*) FROM appart WHERE id = $id");
54 $count = pg_fetch_row($result);
55 pg_free_result($result);
56 // modification d'un appart existant
57
58 if ($count[0] == 1)
59 {
60 // met à  jour tous les champs
61 foreach ($champs as $champ => $type)
62 {
63 $champSQL = getChampSQL($inputs[$champ], $champ, $type);
64 if (!@pg_query("UPDATE appart SET ".$champ." = ".$champSQL." WHERE id = $id"))
65 $erreur = pg_last_error();
66 #echo "UPDATE appart SET ".$champ." = ".$champSQL." WHERE id = $id<br/>";
67 }
68 }
69 // ajout d'un nouvel appart
70 else
71 {
72 $champsSQL = '';
73 $valuesSQL = '';
74 $debut = true;
75 foreach ($champs as $champ => $type)
76 {
77 $champSQL = getChampSQL($inputs[$champ], $champ, $type);
78 $champsSQL .= ($debut ? '' : ',') . $champ;
79 $valuesSQL .= ($debut ? '' : ',') . $champSQL;
80 $debut = false;
81 }
82 if(!@pg_query("INSERT INTO appart (".$champsSQL.") VALUES(".$valuesSQL.")"))
83 {
84 $erreur = pg_last_error();
85 $action = "ajouter";
86 }
87 #echo "INSERT INTO appart (".$champsSQL.") VALUES(".$valuesSQL.")";
88 }
89 }
90 else if ($action == 'supprimer')
91 {
92 @pg_query("DELETE FROM appart WHERE id = $id");
93 $id = 1;
94 }
95 ?>
96