PHP Lazy Person function automatically add data _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/*
*@ Adding data functions automatically
*@ $table Table Name
*@ $arr Word Snippets Array ("title", Array ("content", int))
*@ Array (field, type)
*@ type description
html--Allow HTML
unhtml-HTML is not allowed
int--int Type
Float--Float type
*/
automatically inserting 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]))
{
equals 1 to save HTML by default does 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]))
{
equals 1 to save HTML by default does 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 ();
}
/**
@ Auto Update data function
*@ $table Table Name
*@ $arr Word Snippets Array ("title", Array ("content", int))
*@ Array (field, type)
*@ type description
html--Allow HTML
unhtml-HTML is not allowed
int--int Type
Float--Float type
* * $where condition array type same as $arr
* How to $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]))
{
equals 1 to save HTML by default does 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]))
{
equals 1 to save HTML by default does 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;
Parse 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]))
{
equals 1 to save HTML by default does 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]))
{
equals 1 to save HTML by default does 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);
}

http://www.bkjia.com/PHPjc/323747.html www.bkjia.com true http://www.bkjia.com/PHPjc/323747.html techarticle Copy the code as follows:/* *@ automatically add data function *@ $table table name *@ $arr Word Snippets Array ("title", Array ("content", int)) *@ Array (field, type) *@ type description html--allow HT ...

  • Related Article

    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.