Php lazy functions automatically add data
Last Update:2014-07-28
Source: Internet
Author: User
Php lazy functions automatically add data. For more information, see.
Copy code The 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 );
}