4 * Renvoie la valeur "SQL" d'un champ.
8 function getChampSQL($valeur, $champ, $type)
10 $champSQL = trim($valeur);
11 if ($champ == 'statut' && $champSQL == '')
13 if ($type == 'int' && ($champSQL == '' OR !is_numeric($champSQL)))
15 if ($type == 'date' && $champSQL == '')
17 if ($type == 'timestamp' && $champSQL == '')
20 if ($champSQL != 'NULL')
21 $champSQL = "'".(str_replace("'", "''", $champSQL))."'";
26 $inputs = array_merge($_GET, $_POST);
30 // l'id de l'appart afficher, à modifier, à ajouter ou à supprimer
32 if (array_key_exists('id', $inputs))
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
41 if (array_key_exists('action', $inputs))
42 $action = strtolower($inputs['action']);
44 if ($action == 'ajouter')
46 $result = pg_query("SELECT nextval('appart_id_seq')");
47 $appartData = pg_fetch_row($result);
49 pg_free_result($result);
51 else if ($action == 'mémoriser')
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
60 // met à jour tous les champs
61 foreach ($champs as $champ => $type)
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/>";
69 // ajout d'un nouvel appart
75 foreach ($champs as $champ => $type)
77 $champSQL = getChampSQL($inputs[$champ], $champ, $type);
78 $champsSQL .= ($debut ? '' : ',') . $champ;
79 $valuesSQL .= ($debut ? '' : ',') . $champSQL;
82 if(!@pg_query("INSERT INTO appart (".$champsSQL.") VALUES(".$valuesSQL.")"))
84 $erreur = pg_last_error();
87 #echo "INSERT INTO appart (".$champsSQL.") VALUES(".$valuesSQL.")";
90 else if ($action == 'supprimer')
92 @pg_query("DELETE FROM appart WHERE id = $id");