Php lazy functions automatically add data

Source: Internet
Author: User

Copy codeThe Code is as follows :/*
* @ Automatically add data Functions
* @ $ Table Name
* @ $ Arr field library array ("title", array ("content", int ))
* @ Array (field, type)
* @ Type description
Html -- allow html
Unhtml-html not allowed
Int -- int type
Float -- float Type
*/
// Automatic data insertion function
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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 Name
* @ $ Arr field library array ("title", array ("content", int ))
* @ Array (field, type)
* @ Type description
Html -- allow html
Unhtml-html not allowed
Int -- int type
Float -- float Type
** $ Where condition array is of the same type as $ arr
* $ Method form submission method
*/
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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])
{
// If the value is equal to 1, html is saved. html is not saved by default.
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.