PHP lazy people function to automatically add data _php tips

Source: Internet
Author: User
Copy Code code 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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.