From 8cf9f76d347a6f880252c944bc41b7387c9a2481 Mon Sep 17 00:00:00 2001 From: luofei614 Date: Sun, 8 Jul 2012 21:40:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0db=5Finsert,db=5Fupdate?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _lp/core/lib/db.function.php | 94 ++++++++++++++++++++++++++++++++ _lp/core/lib/db.sae.function.php | 94 ++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) diff --git a/_lp/core/lib/db.function.php b/_lp/core/lib/db.function.php index a2c044b..b4993c9 100644 --- a/_lp/core/lib/db.function.php +++ b/_lp/core/lib/db.function.php @@ -158,4 +158,98 @@ function close_db( $db = NULL ) mysql_close( $db ); } +// ================================================================== +// +// 插入数据 +// +// ------------------------------------------------------------------ + + +function db_insert($table,$data,$replace=false){ + if(is_string($data)){ + $data=db_create($data); + } + $keys=array_keys($data); + $values=array_values($data); + $values=array_map('s', $values);//安全过滤 + $type=$replace?'REPLACE':'INSERT'; + return run_sql("{$type} INTO `{$table}`(`".implode("`,`", $keys)."`) VALUES('".implode("','", $values)."')"); +} + +// ================================================================== +// +// 更新数据 +// +// ------------------------------------------------------------------ + +function db_update($table,$data,$where){ + if(is_string($data)){ + $data=db_create($data); + } + $fields=array(); + foreach($data as $key=>$value){ + $fields[]="`{$key}`='".s($value)."'";//安全过滤 + } + return run_sql("UPDATE `{$table}` SET ".implode(',', $fields)." WHERE {$where}"); +} + +// ================================================================== +// +// 创建数据 +// $data的格式: +// field1,field2,field3 +// or +// fiedl1,field2,field3:fun // 使用过滤函数过滤 +// or +// field1,^field2,field3|fun,filed4:fun +// or +//!field1,field2,filed3:fun +// +// ------------------------------------------------------------------ + +function db_create($data){ + //判断是否为非模式,字符串以!开头 + if('!'==substr($data, 0,1)){ + $not=true; + //如果为非模式,去掉开头的! + $data=substr($data,1); + }else{ + $not=false; + } + $field_filter=explode(':', $data); + //提取字段 + $fields=explode(',', $field_filter[0]); + //提取过滤函数 + $filter=isset($field_filter[1])?$field_filter[1]:false; + $postData=$_POST; + if($not){//非模式的处理 + foreach ($fields as $field ){ + unset($postData[$field]); + } + if($filter) $postData=array_map($filter, $postData); + return $postData; + } + $result=array(); + //循环字段 + foreach ($fields as $field) { + $not_filter=false; + //字段如以^开头,不进行过滤函数过滤。 + if('^'==substr($field, 0,1)){ + $not_filter=true; + $field=substr($field, 1); + } + //字段如果有|隔开一个函数名还需要用单独的过滤函数过滤 + $field_single=explode('|', $field); + $field=$field_single[0]; + $single_filter=isset($field_single[1])?$field_single[1]:false; + //数据过滤 + if($single_filter) $postData[$field]=$single_filter($postData[$field]); + if($filter) $postData[$field]=$filter($postData[$field]); + //加入result + $result[$field]=$postData[$field]; + } + return $result; + +} + ?> \ No newline at end of file diff --git a/_lp/core/lib/db.sae.function.php b/_lp/core/lib/db.sae.function.php index b61b703..abcba3b 100644 --- a/_lp/core/lib/db.sae.function.php +++ b/_lp/core/lib/db.sae.function.php @@ -200,4 +200,98 @@ function close_db( $db = NULL ) mysql_close( $db ); } +// ================================================================== +// +// 插入数据 +// +// ------------------------------------------------------------------ + + +function db_insert($table,$data,$replace=false){ + if(is_string($data)){ + $data=db_create($data); + } + $keys=array_keys($data); + $values=array_values($data); + $values=array_map('s', $values);//安全过滤 + $type=$replace?'REPLACE':'INSERT'; + return run_sql("{$type} INTO `{$table}`(`".implode("`,`", $keys)."`) VALUES('".implode("','", $values)."')"); +} + +// ================================================================== +// +// 更新数据 +// +// ------------------------------------------------------------------ + +function db_update($table,$data,$where){ + if(is_string($data)){ + $data=db_create($data); + } + $fields=array(); + foreach($data as $key=>$value){ + $fields[]="`{$key}`='".s($value)."'";//安全过滤 + } + return run_sql("UPDATE `{$table}` SET ".implode(',', $fields)." WHERE {$where}"); +} + +// ================================================================== +// +// 创建数据 +// $data的格式: +// field1,field2,field3 +// or +// fiedl1,field2,field3:fun // 使用过滤函数过滤 +// or +// field1,^field2,field3|fun,filed4:fun +// or +//!field1,field2,filed3:fun +// +// ------------------------------------------------------------------ + +function db_create($data){ + //判断是否为非模式,字符串以!开头 + if('!'==substr($data, 0,1)){ + $not=true; + //如果为非模式,去掉开头的! + $data=substr($data,1); + }else{ + $not=false; + } + $field_filter=explode(':', $data); + //提取字段 + $fields=explode(',', $field_filter[0]); + //提取过滤函数 + $filter=isset($field_filter[1])?$field_filter[1]:false; + $postData=$_POST; + if($not){//非模式的处理 + foreach ($fields as $field ){ + unset($postData[$field]); + } + if($filter) $postData=array_map($filter, $postData); + return $postData; + } + $result=array(); + //循环字段 + foreach ($fields as $field) { + $not_filter=false; + //字段如以^开头,不进行过滤函数过滤。 + if('^'==substr($field, 0,1)){ + $not_filter=true; + $field=substr($field, 1); + } + //字段如果有|隔开一个函数名还需要用单独的过滤函数过滤 + $field_single=explode('|', $field); + $field=$field_single[0]; + $single_filter=isset($field_single[1])?$field_single[1]:false; + //数据过滤 + if($single_filter) $postData[$field]=$single_filter($postData[$field]); + if($filter) $postData[$field]=$filter($postData[$field]); + //加入result + $result[$field]=$postData[$field]; + } + return $result; + +} + ?> \ No newline at end of file