php懒人函数 自动添加数据
作者:bea
代码如下: /* *@自动添加数据函数 *@$table 表名 *@$arr 字段库 array("title",array("content",int)) *@ array(字段,类型) *@ 类型说明 html--允许html unhtml-不允许html int --int类型 float -- float 类型 */ //自动插入数据函数 function autoInsert($table,$arr=array(),$method='p
代码如下:
/*
*@自动添加数据函数
*@$table 表名
*@$arr 字段库 array("title",array("content",int))
*@ array(字段,类型)
*@ 类型说明
html--允许html
unhtml-不允许html
int --int类型
float -- float 类型
*/
//自动插入数据函数
function autoInsert($table,$arr=array(),$method='post')
{
$sql="insert into ".DB_TBLPRE."$table set ";
$var="";
print_r($arr);
if(empty($arr)) $arr=$_POST?$_POST:$_GET;
if(empty($arr)) return false;
$ct=count($arr)-1;
foreach($arr as $k=> $v)
{
$vtype="unhtml";
if(is_array($v))
{
$vtype=@$v[1];
$v=$v[0];
}
if($method=='post')
{
$_POST[$v]=isset($_POST[$v])?trim($_POST[$v]):"";
if(is_int($_POST[$v]))
{
$_POST[$v]=intval($_POST[$v]);
}elseif(is_float($_POST[$v]))
{
$_POST[$v]=floatval($_POST[$v]);
}elseif(is_string($_POST[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=="unhtml")
{
$_POST[$v]=htmlspecialchars($_POST[$v]);
}elseif($vtype=="int")
{
$_POST[$v]=@intval($_POST[$v]);
}elseif($vtype=='float')
{
$_POST[$v]=@floatval($_POST[$v]);
}
}
$var.= "$v = '$_POST[$v]' ".($k<$ct?",":"");
}else
{
$_GET[$v]=isset($_GET[$v])?trim($_GET[$v]):"";
if(is_int($_GET[$v]))
{
$_GET[$v]=intval($_GET[$v]);
}elseif(is_float($_GET[$v]))
{
$_GET[$v]=floatval($_GET[$v]);
}elseif(is_string($_GET[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=='unhtml')
{
$_GET[$v]=htmlspecialchars($_GET[$v]);
}elseif($vtype=='int')
{
$_GET[$v]=intval($_GET[$v]);
}elseif($vtype=='float')
{
$_GET[$v]=floatval($_GET[$v]);
}
}
$var .="$v= '$_GET[$v]' ".($k<$ct?",":"");
}
}
$sql.=$var;
$this->query($sql);
return $this->insert_id();
}
/**
@自动更新数据函数
*@$table 表名
*@$arr 字段库 array("title",array("content",int))
*@ array(字段,类型)
*@ 类型说明
html--允许html
unhtml-不允许html
int --int类型
float -- float 类型
** $where 条件数组 类型同 $arr一样
*$method 表单提交的方式
*/
function autoUpdate($table,$arr=array(),$where=array(),$method='post')
{
$sql="update ".DB_TBLPRE."$table set ";
$var=$w="";
if(empty($arr)) $arr=$_POST?$_POST:$_GET;
if(empty($arr)) return false;
$ct=count($arr)-1;
foreach($arr as $k=> $v)
{
$vtype="unhtml";
if(is_array($v))
{
$vtype=@$v[1];
$v=$v[0];
}
if($method=='post')
{
$_POST[$v]=isset($_POST[$v])?trim($_POST[$v]):"";
if(is_int($_POST[$v]))
{
$_POST[$v]=intval($_POST[$v]);
}elseif(is_float($_POST[$v]))
{
$_POST[$v]=floatval($_POST[$v]);
}elseif(is_string($_POST[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=="unhtml")
{
$_POST[$v]=htmlspecialchars($_POST[$v]);
}elseif($vtype=="int")
{
$_POST[$v]=@intval($_POST[$v]);
}elseif($vtype=='float')
{
$_POST[$v]=@floatval($_POST[$v]);
}
}
$var.= "$v = '$_POST[$v]' ".($k<$ct?",":"");
}else
{
$_GET[$v]=isset($_GET[$v])?trim($_GET[$v]):"";
if(is_int($_GET[$v]))
{
$_GET[$v]=intval($_GET[$v]);
}elseif(is_float($_GET[$v]))
{
$_GET[$v]=floatval($_GET[$v]);
}elseif(is_string($_GET[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=='unhtml')
{
$_GET[$v]=htmlspecialchars($_GET[$v]);
}elseif($vtype=='int')
{
$_GET[$v]=intval($_GET[$v]);
}elseif($vtype=='float')
{
$_GET[$v]=floatval($_GET[$v]);
}
}
$var .="$v= '$_GET[$v]' ".($k<$ct?",":"");
}
}
$sql.=$var;
//解析 where
$ct=count($where)-1;
if(!empty($where)) $w=" where ";
foreach($where as $k=> $v)
{
$vtype="unhtml";
if(is_array($v))
{
$vtype=@$v[1];
$v=$v[0];
}
if($method=='post')
{
$_POST[$v]=isset($_POST[$v])?trim($_POST[$v]):"";
if(is_int($_POST[$v]))
{
$_POST[$v]=intval($_POST[$v]);
}elseif(is_float($_POST[$v]))
{
$_POST[$v]=floatval($_POST[$v]);
}elseif(is_string($_POST[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=="unhtml")
{
$_POST[$v]=htmlspecialchars($_POST[$v]);
}elseif($vtype=="int")
{
$_POST[$v]=@intval($_POST[$v]);
}elseif($vtype=='float')
{
$_POST[$v]=@floatval($_POST[$v]);
}
}
$w.= "$v = '$_POST[$v]' ".($k<$ct?" and ":"");
}else
{
$_GET[$v]=isset($_GET[$v])?trim($_GET[$v]):"";
if(is_int($_GET[$v]))
{
$_GET[$v]=intval($_GET[$v]);
}elseif(is_float($_GET[$v]))
{
$_GET[$v]=floatval($_GET[$v]);
}elseif(is_string($_GET[$v]))
{
//等于1 为保存html 默认不保存html
if($vtype=='unhtml')
{
$_GET[$v]=htmlspecialchars($_GET[$v]);
}elseif($vtype=='int')
{
$_GET[$v]=intval($_GET[$v]);
}elseif($vtype=='float')
{
$_GET[$v]=floatval($_GET[$v]);
}
}
$w .="$v= '$_GET[$v]' ".($k<$ct?" and ":"");
}
}
$sql.=$w;
$this->query($sql);
}
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- PHP和Mysqlweb应用开发核心技术 第1部分 Php基础-1 开始了解php
- PHP的范围解析操作符(::)的含义分析说明
- 一个基于PDO的数据库操作类(新) 一个PDO事务实例
- PHP array_multisort()函数的使用札记
- PHP导出MySQL数据到Excel文件(fputcsv)
- PHP include_path设置技巧分享
- php array_intersect比array_diff快(附详细的使用说明)
- php header Content-Type类型小结
- php中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
- PHP采集利器 Snoopy 试用心得
- php中的三元运算符使用说明
- PHP 数据结构 算法 三元组 Triplet
- php中批量删除Mysql中相同前缀的数据表的代码
- PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
- PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
- 在php中判断一个请求是ajax请求还是普通请求的方法
- php编程实现获取excel文档内容的代码实例
- PHP学习笔记之数组篇
- php设计模式 Visitor 访问者模式