1 |
<? |
2 |
/* |
3 |
* Phormation |
4 |
* - A library of PHP code to make development of database-driven |
5 |
* html forms easy and quick |
6 |
* |
7 |
* Copyright (C) 2000 Jason D. Hildebrand |
8 |
* PeaceWorks Computer Consulting |
9 |
* |
10 |
* jason@peaceworks.ca |
11 |
* |
12 |
* This program is free software; you can redistribute it and/or modify |
13 |
* it under the terms of the GNU General Public License as published by |
14 |
* the Free Software Foundation; either version 2 of the License, or |
15 |
* (at your option) any later version. |
16 |
* |
17 |
* This program is distributed in the hope that it will be useful, |
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
* GNU General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU General Public License |
23 |
* along with this program; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. |
25 |
*/ |
26 |
|
27 |
|
28 |
include_once( "$phormationdir/dbi.php" ); |
29 |
|
30 |
function quote_if_string( $value ) |
31 |
{ |
32 |
if( is_string( $value ) ) { |
33 |
$value = "'" . $value . "'"; |
34 |
} |
35 |
return $value; |
36 |
} |
37 |
|
38 |
function fetchrecord( $tablename, $tablekey, $fields, $record_id ) { |
39 |
global $conn; |
40 |
|
41 |
$query = "select "; |
42 |
for( $i = 0; $i < count( $fields ) - 1; $i++ ) { |
43 |
$query .= $fields[$i] . ", "; |
44 |
} |
45 |
$query .= $fields[$i] . " from $tablename where $tablekey = "; |
46 |
$query .= quote_if_string( $record_id ); |
47 |
//echo $query . '<BR>'; |
48 |
$result = dbi_exec( $conn, $query ); |
49 |
return( $result ); |
50 |
} |
51 |
|
52 |
function deleterecord( $tablename, $tablekey, $record_id ) { |
53 |
global $conn; |
54 |
|
55 |
if (!$record_id) return; |
56 |
$query = "delete from $tablename where $tablekey = $record_id"; |
57 |
$result = dbi_exec( $conn, $query ); |
58 |
return( $result ); |
59 |
} |
60 |
|
61 |
function insertvalue( $fieldname, $value, $sql ) |
62 |
{ |
63 |
$sql[0][] = $fieldname; |
64 |
$sql[1][] = $value; |
65 |
} |
66 |
|
67 |
function convert_nl( $str ) |
68 |
{ |
69 |
if ( !$str ) return $str; |
70 |
# return preg_replace('/\s*(\r\n|\n\r|\r|\n)/m', '<br>', $str); |
71 |
return $str; |
72 |
} |
73 |
|
74 |
function savechanges( $tablename, $tablekey, $record_id, $sql ) |
75 |
{ |
76 |
global $conn; |
77 |
|
78 |
//for( $i = 0; ! empty( $sql[0][$i] ); $i++ ) { |
79 |
//echo "Field: " . $sql[0][$i] . "<BR>"; |
80 |
//echo "Value: " . $sql[1][$i] . "<BR>"; |
81 |
//} |
82 |
|
83 |
// either update or insert the tuple, depending on whether it already exists |
84 |
if( $record_id != "NULL" && $record_id != "" ) { |
85 |
// this record already exists; update it |
86 |
$query = "update $tablename set "; |
87 |
for( $i = 0; ! empty( $sql[0][$i + 1] ); $i++ ) { |
88 |
$query .= $sql[0][$i] . " = " . convert_nl($sql[1][$i]) . ", "; |
89 |
} |
90 |
$query .= $sql[0][$i] . " = " . convert_nl($sql[1][$i]); |
91 |
$query .= " where $tablekey = " . quote_if_string( $record_id ); |
92 |
|
93 |
//echo $query . "<BR>"; |
94 |
$result = dbi_exec( $conn, $query ); |
95 |
} else { |
96 |
// this is a new record; insert it |
97 |
$record_id = ""; |
98 |
|
99 |
$query = "insert into $tablename ( "; |
100 |
for( $i = 0; ! empty( $sql[0][$i + 1] ); $i++ ) { |
101 |
|
102 |
// check to see if this field is the tablekey |
103 |
if( $sql[0][$i] == $tablekey ) { |
104 |
$record_id = $sql[1][$i]; |
105 |
} |
106 |
$query .= $sql[0][$i] . ", "; |
107 |
} |
108 |
|
109 |
// check to see if this field is the tablekey |
110 |
if( $sql[0][$i] == $tablekey ) { |
111 |
$record_id = $sql[1][$i]; |
112 |
} |
113 |
$query .= $sql[0][$i] . " ) values ( "; |
114 |
|
115 |
for( $i = 0; ! empty( $sql[0][$i + 1] ); $i++ ) { |
116 |
$query .= convert_nl($sql[1][$i]) . ", "; |
117 |
} |
118 |
$query .= convert_nl($sql[1][$i]) . " )"; |
119 |
//echo $query . "<BR>"; |
120 |
$result = dbi_exec( $conn, $query ); |
121 |
if( ! $result ) { |
122 |
dbi_error( $conn ); |
123 |
} else { |
124 |
if( empty( $record_id ) ) { |
125 |
$record_id = dbi_getlastid( $conn, $result, $tablename, $tablekey ); |
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
?> |