Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:23:04+00:00 2026-05-13T11:23:04+00:00

Below is part of a PHP database class someone else wrote, I have removed

  • 0

Below is part of a PHP database class someone else wrote, I have removed about 80% of it’s code, all the un-related code to my question has been removed and just the amount remains that allows me to test this class without actually hitting a real database.

This class has a couple methods that let you set a key and value it then turns it into a mysql UPDATE and INSERT sql query using an array. I am trying to figure out how to use this code 100% so I can use this feature of it for UPDATE and INSERTS in my own application.

Basicly from what I gather you do something like this…

// assign some key/values to insert into DB
$db->assign('name', 'dfgd');
$db->assign('age', 87);
$db->assign('sex', 'female');
$db->assign('user_id', 4556);

// Do  the insert
$db->insert('testing2');

Now where I am confused is I can keep on running code like this over and over on the page and it always will use the correct set of key/value array sets. Above you can see I used the assign() method 4 times and then call the insert() method which creates this

INSERT INTO test (name, age, sex, user_id) VALUES (jason davis, 26, male, 5345)

Now if I run another set like this on the same page…

// assign some key/values to insert into DB
$db->assign('name', 'dfgd');
$db->assign('age', 87);
$db->assign('sex', 'female');
$db->assign('user_id', 4556);

// Do  the insert
$db->insert('testing2');

It then creates this…

INSERT INTO testing2 (name, age, sex, user_id) VALUES (dfgd, 87, female, 4556)

So how does it not combine the 2 sets of 4, so instead of inserting 8 record on the second insert, it completey replaces the first set of 4 values with the new set. This is great and what I want but I do not understand how it is happening? Also can this be improved anyway?

Below is a full class and my demo code, it can be ran without needing to connect to mysql for this demo, it will print to screen the SQL that it builds.

Also where would the public function reset() in the code below need to be used at, or would it not be needed?

<?php 
class DB{
    public $fields;

    public function assign($field, $value){
        $this->fields[$field] = ($value)==""?("'".$value."'"):$value;
    }

    public function assign_str($field, $value){
        $this->fields[$field] = "'".addslashes($value)."'";
    }

    public function reset(){
        $this->fields = array();
    }

    public function insert($table){
        $f = "";
        $v = "";
        reset($this->fields);
        foreach($this->fields as $field=>$value){
            $f.= ($f!=""?", ":"").$field;
            $v.= ($v!=""?", ":"").$value;
        }
        $sql = "INSERT INTO ".$table." (".$f.") VALUES (".$v.")";
        //print SQL to screen for testing
        echo $sql;
        //$this->query($sql);
        return $this->insert_id();
    }

    public function update($table, $where){
        $f = "";
        reset($this->fields);
        foreach($this->fields as $field=>$value){
            $f.= ($f!=""?", ":"").$field." = ".$value;
        }
        $sql = "UPDATE ".$table." SET ".$f." ".$where;
        echo $sql;
        //$this->query($sql);
    }

    public function query($_query){
        $this->query = $_query;
        $this->result = @mysql_query($_query, $this->link_id) or die( $_query."<p>".mysql_error($this->link_id) );
        return $this->result;
    }

    public function insert_id(){
        return @mysql_insert_id($this->link_id);
    }
}


// start new DB object
$db = new DB;

// assign some key/values to insert into DB
$db->assign('name', 'jason davis');
$db->assign('age', 26);
$db->assign('sex', 'male');
$db->assign('user_id', 5345);

// Do  the insert
$db->insert('test');


echo '<hr />';

// assign some key/values to insert into DB
$db->assign('name', 'dfgd');
$db->assign('age', 87);
$db->assign('sex', 'female');
$db->assign('user_id', 4556);

// Do  the insert
$db->insert('testing2');


echo '<hr />';

// assign some key/values to UPDATE the DB
$db->assign('name', 'jason davis');
$db->assign('age', 26);
$db->assign('sex', 'male');
$db->assign('user_id', 5345);

// DO the DB UPDATE
$db->update('blogs', 'WHERE user_id = 23');

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:23:04+00:00Added an answer on May 13, 2026 at 11:23 am

    insert() and update() should (originally) set the $this->fields property back to an empty array upon execution, but you somehow (wrongly) deleted that code?

    Update your code to this:

        public function insert($table){
            $f = "";
            $v = "";
            foreach($this->fields as $field=>$value){
                $f.= ($f!=""?", ":"").$field;
                $v.= ($v!=""?", ":"").$value;
            }
            $sql = "INSERT INTO ".$table." (".$f.") VALUES (".$v.")";
            $this->reset();
            //print SQL to screen for testing
            echo $sql;
            //$this->query($sql);
            return $this->insert_id();
        }
    
        public function update($table, $where){
            $f = "";
            foreach($this->fields as $field=>$value){
                $f.= ($f!=""?", ":"").$field." = ".$value;
            }
            $sql = "UPDATE ".$table." SET ".$f." ".$where;
            $this->reset();
            echo $sql;
            //$this->query($sql);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 305k
  • Answers 305k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One advantage of the ProcessedDate is null predicate is that… May 13, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer I don't know the game but from your description it… May 13, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer I think you can do this with ItemsControl + ItemsPanelTemplate.… May 13, 2026 at 9:07 pm

Related Questions

Sorry for this long post. The question is however small but requires full detail.
We have a local database of our products without the price. To get the
I found this script on about.com which I'm trying to learn from on how
I am working on a website using a PHP script to display information formatted
I am working on a project using Drupal 6 (6.11 at the moment, haven't

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.