php懒人函数 自动添加数据

高洛峰
Libérer: 2023-03-01 09:44:01
original
1357 Les gens l'ont consulté

复制代码 代码如下: 

/* 
*@自动添加数据函数 
*@$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); 
}
Copier après la connexion


Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!