欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php懒人函数 自动添加数据

程序员文章站 2024-02-04 22:04:16
复制代码 代码如下: /* *@自动添加数据函数 *@$table 表名 *@$arr 字段库 array("title",array("content",int)) *@...
复制代码 代码如下:

/*
*@自动添加数据函数
*@$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);
}