Home php教程 php手册 php懒人函数 自动添加数据

php懒人函数 自动添加数据

Jun 21, 2016 am 08:54 AM
elseif float get post

复制代码 代码如下:


/*
*@自动添加数据函数
*@$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}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}
}
$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}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}
}
$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}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}
}
$sql.=$w;
$this->query($sql);
}



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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

What is the maximum value of float?

How to automate tasks using PowerShell How to automate tasks using PowerShell Feb 20, 2024 pm 01:51 PM

How to automate tasks using PowerShell

A brief analysis of the POST method in PHP with parameters to jump to the page A brief analysis of the POST method in PHP with parameters to jump to the page Mar 23, 2023 am 09:15 AM

A brief analysis of the POST method in PHP with parameters to jump to the page

How to determine whether a post has been submitted in PHP How to determine whether a post has been submitted in PHP Mar 21, 2023 pm 07:12 PM

How to determine whether a post has been submitted in PHP

How to use python requests post How to use python requests post Apr 29, 2023 pm 04:52 PM

How to use python requests post

How does java initiate an http request and call the post and get interfaces? How does java initiate an http request and call the post and get interfaces? May 16, 2023 pm 07:53 PM

How does java initiate an http request and call the post and get interfaces?

How to solve the problem that NGINX reverse proxy returns 405 for POST request of HTML page How to solve the problem that NGINX reverse proxy returns 405 for POST request of HTML page May 22, 2023 pm 07:49 PM

How to solve the problem that NGINX reverse proxy returns 405 for POST request of HTML page

How to implement PHP to jump to the page and carry POST data How to implement PHP to jump to the page and carry POST data Mar 22, 2024 am 10:42 AM

How to implement PHP to jump to the page and carry POST data

See all articles