1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @package fwolflib
|
4
|
|
|
* @copyright Copyright 2006, Fwolf
|
5
|
|
|
* @author Fwolf, [email protected]
|
6
|
|
|
* @since 2006-07-08
|
7
|
|
|
* @version $Id$
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php');
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* 向数据库中写入数据
|
16
|
|
|
* 根据指定的主键或联合主键自动判断是insert还是update
|
17
|
|
|
* 当然主键必须有值包含在数据中
|
18
|
|
|
*
|
19
|
|
|
* @param object $db ADOdb数据库连接对象
|
20
|
|
|
* @param string $tbl 写入数据表名
|
21
|
|
|
* @param array $data 要写入的数据,数组array(column=>value,...)形式
|
22
|
|
|
* @param mixed $pkey 主键,可以是字符串或数组(也可以是定位数据的其他条件,但要求键和值必须包含在data数组中
|
23
|
|
|
* @return mixed 出错返回错误信息,成功返回1(即处理的行数)
|
24
|
|
|
*/
|
25
|
|
|
function DbWrite($db, $tbl, $data, $pkey)
|
26
|
|
|
{
|
27
|
|
|
//对要写入数据库的内容进行处理,主要是根据字段类型添加引号
|
28
|
|
|
//:TODO:改用缓存机制
|
29
|
|
|
$col = $db->MetaColumns($tbl);
|
30
|
|
|
foreach ($data as $key => $val)
|
31
|
|
|
{
|
32
|
|
|
if (isset($col[strtoupper($key)]))
|
33
|
|
|
{
|
34
|
|
|
//根据字段类型添加引号
|
35
|
|
|
if (!in_array($col[strtoupper($key)]->type, array('int', 'integer', 'tinyint', 'decimal', 'bolean', 'numeric')))
|
36
|
|
|
$data[$key] = "'" . addslashes($val) . "'";
|
37
|
|
|
}
|
38
|
|
|
else
|
39
|
|
|
{
|
40
|
|
|
return("Column $key is not found in db schema.");
|
41
|
|
|
}
|
42
|
|
|
}
|
43
|
|
|
//检查要写入的数据是否存在 insert or update?
|
44
|
|
|
$sql = "select count(1) as c from $tbl where 1=1 ";
|
45
|
|
|
if (is_array($pkey))
|
46
|
|
|
{
|
47
|
|
|
//多个值的定位
|
48
|
|
|
//$s_pkey will be used again when actually write to db
|
49
|
|
|
$s_pkey = '';
|
50
|
|
|
foreach ($pkey as $key)
|
51
|
|
|
{
|
52
|
|
|
//检查键值是否被指定,如果没有被指定,中止操作
|
53
|
|
|
if (isset($data[$key]))
|
54
|
|
|
$s_pkey .= " and $key = $data[$key] ";
|
55
|
|
|
else
|
56
|
|
|
{
|
57
|
|
|
return("Key $key has not assigned a value.");
|
58
|
|
|
}
|
59
|
|
|
}
|
60
|
|
|
$sql .= $s_pkey;
|
61
|
|
|
}
|
62
|
|
|
else
|
63
|
|
|
{
|
64
|
|
|
//单个键值
|
65
|
|
|
$s_pkey = " and $pkey = $data[$pkey] ";
|
66
|
|
|
$sql .= $s_pkey;
|
67
|
|
|
}
|
68
|
|
|
$rs = $db->Execute($sql);
|
69
|
|
|
$i = $rs->fields['c'];
|
70
|
|
|
if (0 == $i)
|
71
|
|
|
{
|
72
|
|
|
//its insert
|
73
|
|
|
$sql = "insert into $tbl (";
|
74
|
|
|
//keys
|
75
|
|
|
foreach ($data as $key=>$val)
|
76
|
|
|
{
|
77
|
|
|
$sql .= "$key, ";
|
78
|
|
|
}
|
79
|
|
|
$sql = substr($sql, 0, strlen($sql) - 2);
|
80
|
|
|
$sql .= ") values (";
|
81
|
|
|
//values
|
82
|
|
|
foreach ($data as $key=>$val)
|
83
|
|
|
{
|
84
|
|
|
$sql .= "$val, ";
|
85
|
|
|
}
|
86
|
|
|
$sql = substr($sql, 0, strlen($sql) - 2);
|
87
|
|
|
$sql .= ")";
|
88
|
|
|
}
|
89
|
|
|
elseif (1 == $i)
|
90
|
|
|
{
|
91
|
|
|
//its update
|
92
|
|
|
$sql = "update $tbl set ";
|
93
|
|
|
foreach ($data as $key=>$val)
|
94
|
|
|
{
|
95
|
|
|
$sql .= "$key = $val, ";
|
96
|
|
|
}
|
97
|
|
|
$sql = substr($sql, 0, strlen($sql) - 2);
|
98
|
|
|
$sql .= " where 1=1 ";
|
99
|
|
|
//use pkey to locate data
|
100
|
|
|
$sql .= $s_pkey;
|
101
|
|
|
}
|
102
|
|
|
else
|
103
|
|
|
{
|
104
|
|
|
//got too many match rows
|
105
|
|
|
return("Got >1 rows by given pkey, which to update ?");
|
106
|
|
|
}
|
107
|
|
|
//finally, write to database
|
108
|
|
|
//$db->debug = true;
|
109
|
|
|
$rs = $db->Execute($sql);
|
110
|
|
|
$i = $db->ErrorNo();
|
111
|
|
|
if (0 == $i)
|
112
|
|
|
{
|
113
|
|
|
//no error
|
114
|
|
|
//echo("1 row writed to database, no error.<br />\n");
|
115
|
|
|
return 1;
|
116
|
|
|
}
|
117
|
|
|
else
|
118
|
|
|
{
|
119
|
|
|
//error occur
|
120
|
|
|
return($db->ErrorMsg());
|
121
|
|
|
}
|
122
|
|
|
} // end of function DbWrite
|
123
|
|
|
|
124
|
|
|
?>
|
|
|
|
|
125
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.