Home > Backend Development > PHP Tutorial > PHP lazy function to automatically add data sample code sharing

PHP lazy function to automatically add data sample code sharing

PHP中文网
Release: 2023-02-28 21:26:02
Original
1249 people have browsed it

phpLazy personfunction automatically adds data, friends in need can refer to it.

The code is as follows:

/* 
*@自动添加数据函数 
*@$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 = &#39;$_POST[$v]&#39; ".($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==&#39;unhtml&#39;) 
{ 
$_GET[$v]=htmlspecialchars($_GET[$v]); 
}elseif($vtype==&#39;int&#39;) 
{ 
$_GET[$v]=intval($_GET[$v]); 
}elseif($vtype==&#39;float&#39;) 
{ 
$_GET[$v]=floatval($_GET[$v]); 
} 
} 
$var .="$v= &#39;$_GET[$v]&#39; ".($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=&#39;post&#39;) 
{ 
$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==&#39;post&#39;) 
{ 
$_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==&#39;float&#39;) 
{ 
$_POST[$v]=@floatval($_POST[$v]); 
} 
} 
$var.= "$v = &#39;$_POST[$v]&#39; ".($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==&#39;unhtml&#39;) 
{ 
$_GET[$v]=htmlspecialchars($_GET[$v]); 
}elseif($vtype==&#39;int&#39;) 
{ 
$_GET[$v]=intval($_GET[$v]); 
}elseif($vtype==&#39;float&#39;) 
{ 
$_GET[$v]=floatval($_GET[$v]); 
} 
} 
$var .="$v= &#39;$_GET[$v]&#39; ".($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==&#39;post&#39;) 
{ 
$_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==&#39;float&#39;) 
{ 
$_POST[$v]=@floatval($_POST[$v]); 
} 
} 
$w.= "$v = &#39;$_POST[$v]&#39; ".($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==&#39;unhtml&#39;) 
{ 
$_GET[$v]=htmlspecialchars($_GET[$v]); 
}elseif($vtype==&#39;int&#39;) 
{ 
$_GET[$v]=intval($_GET[$v]); 
}elseif($vtype==&#39;float&#39;) 
{ 
$_GET[$v]=floatval($_GET[$v]); 
} 
} 
$w .="$v= &#39;$_GET[$v]&#39; ".($k<$ct?" and ":""); 
} 
} 
$sql.=$w; 
$this->query($sql); 
}
Copy after login

The above is the sample code shared by PHP lazy function to automatically add data. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template