TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-30-2009, 10:10 PM   #81 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

A little update about the functions. They look like this right now:
php Code:
public function getInsert($table, $args)
        {
           
            $field_names = array_keys($args);
            $field_names = array_map(create_function('$var', 'return "`" . $var . "`";'), $field_names);
           
            $sql_string_names = '(';
            $sql_string_names .= implode(',', $field_names);
            $sql_string_names .= ')';
           
            $exceptions = array('now()', 'null');
            $field_values = array();
           
            foreach($args as $field_value)
            {
               
                $field_value = trim($field_value);
                $field_value = strtolower($field_value);
               
                if(!in_array($field_value, $exceptions))
                {
                   
                    if($field_value == '"now()"')
                    {
                       
                        $field_value = "'".str_replace('"', '', $field_value)."'";
                       
                    }
                    elseif($field_value == '"null"')
                    {
                       
                        $field_value = "'".str_replace('"', '', $field_value)."'";
                        
                    }
                    else
                    {
                       
                        $field_value = "'".$field_value."'";
                       
                    }
                   
                }
           
                $field_values[] = $this->secure($field_value);
               
            }
           
            $sql_string_values = '(';
            $sql_string_values .= implode(',', $field_values);
            $sql_string_values .= ')';
           
            return 'INSERT INTO `' . $table . '` ' . $sql_string_names . ' VALUES ' . $sql_string_values . ';';
           
        }
       
        public function insert($table, $args)
        {
           
            $sql = $this->getInsert($table, $args);
           
            if($this->exeQuery($sql))
            {
               
                return true;
               
            }
           
            return false;
           
        }

Usage:
PHP Code:
$sql_data = array('comment_post_ID'      => $comment_post_ID,
                  
'comment_author'       => $comment_author,
                  
'comment_author_email' => $comment_author_email// some comments here
                  
'comment_author_url'   => $comment_author_url,
                  
'comment_author_IP'    => $comment_author_IP,   // IP of the author
                  
'comment_date'         => $comment_date,    // Comment date
                  
'comment_date_gmt'     => $comment_date_gmt,
                  
'comment_content'      => $comment_content,
                  
'comment_approved'     => $comment_approved,
                  
'comment_agent'        => $comment_agent,
                  
'comment_type'         => $comment_type,
                  
'comment_parent'       => $comment_parent,
                  
'user_id'              => $user_id);

$db->insert('wp_comments'$sql_data); 
or..
PHP Code:
$sql_data = array('comment_post_ID'      => $comment_post_ID,
                  
'comment_author'       => $comment_author,
                  
'comment_author_email' => $comment_author_email// some comments here
                  
'comment_author_url'   => $comment_author_url,
                  
'comment_author_IP'    => $comment_author_IP,   // IP of the author
                  
'comment_date'         => $comment_date,    // Comment date
                  
'comment_date_gmt'     => $comment_date_gmt,
                  
'comment_content'      => $comment_content,
                  
'comment_approved'     => $comment_approved,
                  
'comment_agent'        => $comment_agent,
                  
'comment_type'         => $comment_type,
                  
'comment_parent'       => $comment_parent,
                  
'user_id'              => $user_id);

$sql $db->getInsert('wp_comments'$sql_data);

//and execute the sql at later moment whenever you wish:
$db->exeQuery($sql); 
Haven't tested function though. Anyone might want to take a glance at it to see possible errors.

And if you then want to, I can implement it.
__________________
Tanax is offline  
Reply With Quote
Old 01-30-2009, 11:24 PM   #82 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You're going to have a whole heap of trouble (perhaps only a small heap) with that function as-is. Just from looking at the code, without testing it live, there are numerous points of either, "hey, that won't work" or "why on earth is this here?"

What's going on with the "now()" and "null" (with and without double quotes)? Exceptions to what? You strtolower all values? You feed a single-quote-wrapped value through the DBmysql::secure method, is the value secured then?

As for implementing this sort of function, keep it out of the current class at least. There's no harm in creating another class for this sort of query building job.
Salathe is offline  
Reply With Quote
Old 01-30-2009, 11:55 PM   #83 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

As said, this was from that site.
I looked at it at a glance. The "now()" and "null" seemed logical, since you want those to be the sql value of now()..

That's true.. ahwell, it was just an idea
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 05:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design