Hello, please sign in or register
You are here: Home

AutoInsert

This is a reusable function allows you to validate and insert data, and return your user defined error messages

[CODE=function AutoInsert]<?php

function AutoInsert($t, $p, $a = array(), $update = false )
{
    /**
      * Validate Insert
     */
    foreach( $a as $k => $o )
    {
        $i = false;
        $l = ereg_replace('\[[0-9]\]', '', $k);
        if( ( def::input_regex( $o ) && ! eregi( def::input_regex( $o ), @$p[$l] ) )
             || ( $o === INPUT_UNIQUE
                    && ( $b = $this->GetOne("SELECT 1 FROM " . $t . " WHERE $l = '" . @$p[$l] . "'") ) != NULL && !$update)
             || ( $o === INPUT_MATCH && @$p[$l] != @$p[$k] ) )
         {
              $i = true;
         }

        if ( $o === INPUT_KEY )
        {
           $where[$l] = @$p[$l]; }
           if ( $o === INPUT_UNIQUE && $b )
           {
                $where[$l] = @$p[$l];
           }
           ( $i && !( $update && $o === INPUT_UNIQUE ) ? $e[$l] = def::input_errors( $o ) : NULL     );
    }

    if ( isset($e) ) {
        $this->validation_errors = $e;
        return false;
    }


    foreach( $p as $k => &$o )
    {
        if ( $a[$k] === INPUT_PASSWORD )
        {
            $o = "MD5('" . $o . "')";
         } else if ( ereg('\[[0-9]\]', $k) || $o == '')
         {
             $o = NULL;
         }
        else if ( $a[$k] !== INPUT_INTEGER ) { $o = '"' . $o . '"';
    }
}

$p = array_filter($p);

/**
 *Insert or update
 */
if ( $update && isset( $where )
     && $this->GetOne("SELECT 1 FROM " . $t . " WHERE " .     $this->implode_with_keys('         AND ', array_intersect_key( $p, $where) ) ) != NULL)
{
      $q = "UPDATE " . $t . " SET " . $this->implode_with_keys(', ', array_diff_key( $p, $where )) . " WHERE " . $this->implode_with_keys(' AND ', array_intersect_key( $p, $where ));
}
else
 {
     $q = "INSERT INTO " . $t . " (`" . implode( '`, `' , array_keys($p) ) . "`, `insert_stamp`)                             VALUES (" . implode(', ', $p) . ", NOW() ) ";
}
[/CODE]
it requires
[CODE=function implode_with_keys()]
<?php
function implode_with_keys($delim, $a, $DELIM = NULL)
{
    foreach( $a as $k => $v )
    {
        $r[] = "`" . $k . "` = " . $DELIM . $v . $DELIM;
    }
    return implode($delim, $r);
}
?>
[/CODE]

Comments

1015maoqiuyun
coach outlet online, http://www.coachoutletonline-store.us.com/
Created 15/10/15
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share