The php lazy function automatically adds data

  • 2020-05-09 18:13:49
  • OfStack

 
/* 
*@ Automatically add data functions  
*@$table  The name of the table  
*@$arr  Field in the library  array("title",array("content",int)) 
*@ array( field , type ) 
*@  Type specification  
html-- allow html 
unhtml- Don't allow html 
int --int type  
float -- float  type  
*/ 
// Automatically inserts data functions  
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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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(); 
} 
/** 
@ Update data functions automatically  
*@$table  The name of the table  
*@$arr  Field in the library  array("title",array("content",int)) 
*@ array( field , type ) 
*@  Type specification  
html-- allow html 
unhtml- Don't allow html 
int --int type  
float -- float  type  
** $where  Conditions of the array   Type with  $arr1 sample  
*$method  Form submission  
*/ 
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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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; 
// parsing  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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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])) 
{ 
// Is equal to the 1  To preserve html  Default not saved 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); 
} 

Related articles: