Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create Update, Delete and Insert Functions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 304
    Comment on it

    Hello,

    Here I am writing the code to create update, insert and delete functions so that we can use it the same functions anywhere in the project.

    <?php
    // function to insert data 
    function insert($table = "", $fieldsArray = array()) {
        return insert_replace ( $table, $fieldsArray, $format, 'REPLACE' );
    }
    function insert_replace($table, $data, $format = null, $type = 'INSERT') {
        if (! in_array ( strtoupper ( $type ), array (
                'REPLACE',
                'INSERT' 
        ) )) {
            return false;
        }
        $fieldtypes = array ();
        $formats = $format = ( array ) $format;
        $fields = array_keys ( $data );
        $formatted_fields = array ();
        foreach ( $fields as $field ) {
            if (! empty ( $format )) {
                $form = ($form = array_shift ( $formats )) ? $form : $format [0];
            } elseif (isset ( $fieldtypes [$field] )) {
                $form = $fieldtypes [$field];
            } else {
                $form = '%s';
            }
            $formatted_fields [] = $form;
        }
        $sql = "{$type} INTO `$table` (`" . implode ( '`,`', $fields ) . "`) VALUES (" . implode ( ",", $formatted_fields ) . ")";
        return mysql_query ( prepare ( $sql, $data ) );
    }
    // prepare data..
    function prepare($query, $args) {
        if (is_null ( $query )) {
            return;
        }
        $args = func_get_args ();
        array_shift ( $args );
        // If args were passed as an array (as in vsprintf), move them up
        if (isset ( $args [0] ) && is_array ( $args [0] )) {
            $args = $args [0];
        }
        $query = str_replace ( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
        $query = str_replace ( '"%s"', '%s', $query ); // doublequote unquoting
        $query = preg_replace ( '|(?<!%)%f|', '%F', $query ); // Force floats to be locale unaware
        $query = preg_replace ( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
        return @vsprintf ( $query, $args );
    }
    
    // function to update data
        function update($table, $data, $where, $format = null, $where_format = null) {
            if (! is_array ( $data ) || ! is_array ( $where ))
                return false;
            $field_types = array ();
            $formats = $format = ( array ) $format;
            $bits = $wheres = array ();
            foreach ( ( array ) array_keys ( $data ) as $field ) {
                if (! empty ( $format ))
                    $form = ($form = array_shift ( $formats )) ? $form : $format [0];
                elseif (isset ( $field_types [$field] ))
                    $form = $field_types [$field];
                else
                    $form = '%s';
                $bits [] = "`$field` = {$form}";
            }
            $where_formats = $where_format = ( array ) $where_format;
            foreach ( ( array ) array_keys ( $where ) as $field ) {
                if (! empty ( $where_format ))
                    $form = ($form = array_shift ( $where_formats )) ? $form : $where_format [0];
                elseif (isset ( $field_types [$field] ))
                    $form = $field_types [$field];
                else
                    $form = '%s';
                $wheres [] = "`$field` = {$form}";
            }
            $sql = "UPDATE `$table` SET " . implode ( ', ', $bits ) . ' WHERE ' . implode ( ' AND ', $wheres );
            // echo prepare($sql, array_merge(array_values($data),array_values($where)));die;
            return mysql_query ( prepare ( $sql, array_merge ( array_values ( $data ), array_values ( $where ) ) ) );
        }
    
    // function to delete data
    function delete($table, $where, $where&#95;format = null) {
        if (! is_array ( $where ))
            return false;
    
        $bits = $wheres = array ();
        $field_types = array ();
    
        $where_formats = $where_format = ( array ) $where_format;
    
        foreach ( array_keys ( $where ) as $field ) {
            if (! empty ( $where_format )) {
                $form = ($form = array_shift ( $where_formats )) ? $form : $where_format [0];
            } elseif (isset ( $field_types [$field] )) {
                $form = $field_types [$field];
            } else {
                $form = '%s';
            }
    
            $wheres [] = "$field = $form";
        }
    
        $sql = "DELETE FROM $table WHERE " . implode ( ' AND ', $wheres );
        return mysql_query ( prepare ( $sql, $where ) );
    }
    
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: