PHP lazy people function to automatically add data
Last Update:2017-02-28
Source: Internet
Author: User
Copy Code The code is as follows:
/*
*@ automatically add data functions
*@ $table Table Name
*@ $arr Word Genku Array ("title", Array ("content", int))
*@ Array (field, type)
*@ type description
html--allows HTML
unhtml-does not allow HTML
int--int Type
Float--Float type
*/
Inserting data functions automatically
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]))
{
Equal to 1 Save HTML by default do not save 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]))
{
Equal to 1 Save HTML by default do not save 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 ();
}
/**
@ Automatically update data functions
*@ $table Table Name
*@ $arr Word Genku Array ("title", Array ("content", int))
*@ Array (field, type)
*@ type description
html--allows HTML
unhtml-does not allow HTML
int--int Type
Float--Float type
* * $where condition array type same as $arr
* How to submit $method form
*/
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]))
{
Equal to 1 Save HTML by default do not save 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]))
{
Equal to 1 Save HTML by default do not save 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]))
{
Equal to 1 Save HTML by default do not save 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]))
{
Equal to 1 Save HTML by default do not save 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);
}