Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array
- (
- [nome] => Nome
- [sobrenome] => Sobrenome
- [genero] => 1
- [email] => email@mail.com
- )
- INSER INTO tabela (nome, sobrenome, genero, email) VALUES ('$nome', ...)
- foreach ($data as $key => $value){
- $keys = $keys.$key.",";
- $values = $values.$values.",";
- }
- $keys = substr($keys, 0,-1);
- $values = substr($values, 0,-1);
- INSER INTO tabela ('$keys') VALUES ('$values');
- $data = array(
- 'nome' => 'Nome',
- 'sobrenome' => 'Sobrenome',
- 'genero' => 1,
- 'email' => 'email@mail.com'
- );
- $sql = sprintf(
- "INSERT INTO tabela (%s) VALUES ('%s');",
- implode(',', array_keys($data)),
- implode("','", array_values($data))
- );
- // função principal
- function tep_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
- reset($data);
- if ($action == 'insert') {
- $query = 'insert into ' . $table . ' (';
- while (list($columns, ) = each($data)) {
- $query .= $columns . ', ';
- }
- $query = substr($query, 0, -2) . ') values (';
- reset($data);
- while (list(, $value) = each($data)) {
- switch ((string)$value) {
- case 'now()':
- $query .= 'now(), ';
- break;
- case 'null':
- $query .= 'null, ';
- break;
- default:
- $query .= ''' . tep_db_input($value) . '', ';
- break;
- }
- }
- $query = substr($query, 0, -2) . ')';
- } elseif ($action == 'update') {
- $query = 'update ' . $table . ' set ';
- while (list($columns, $value) = each($data)) {
- switch ((string)$value) {
- case 'now()':
- $query .= $columns . ' = now(), ';
- break;
- case 'null':
- $query .= $columns .= ' = null, ';
- break;
- default:
- $query .= $columns . ' = '' . tep_db_input($value) . '', ';
- break;
- }
- }
- $query = substr($query, 0, -2) . ' where ' . $parameters;
- }
- return tep_db_query($query, $link);
- }
- // Funções chamadas
- function tep_db_query($query, $link = 'db_link') {
- global $$link;
- $result = mysqli_query($$link, $query) or tep_db_error($query, mysqli_errno($$link), mysqli_error($$link));
- return $result;
- }
- function tep_db_input($string, $link = 'db_link') {
- global $$link;
- return mysqli_real_escape_string($$link, $string);
- }
- function tep_db_error($query, $errno, $error) {
- die('<font color="#000000"><strong>' . $errno . ' - ' . $error . '<br /><br />' . $query . '<br /><br /><small><font color="#ff0000">[TEP STOP]</font></small><br /><br /></strong></font>');
- }
- $data_array = array(
- 'nome' => 'Nome',
- 'sobrenome' => 'Sobrenome',
- 'genero' => 1,
- 'email' => 'email@mail.com',
- 'fone' => 'null',
- 'data' => 'now()');
- // insert
- tep_db_perform('clientes', $data_array);
- // update
- tep_db_perform('clientes', $data_array, 'update', "cliente_id = '1'");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement