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 7429321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:57:01+00:00 2026-05-29T08:57:01+00:00

I’m building an application where users can add, edit and delete users from the

  • 0

I’m building an application where users can add, edit and delete users from the database.

I’ve done the ‘View Users’ ( Select statement returning joined tables ) and the ‘Add Users’ ( Returns a form for user to fill in and INSERTS into database after validation of course )

But I’m having trouble with the ‘Edit Users’.

My Edit form is the SAME as the add form but the values for the text boxes are filled in with values from the database.

Upon submit, the logic checks for mismatches between the user input and the database values (So we know what we’re actually updating in the database)

So I end up with an array of all the values that need to be changed, example:

$values_to_update = array (
    "telephone" => "07788991010"
    "email_address" => "my_new_email_address@host.com"
);

The values in this array are dynamic and I need to thing of a way to dynamically update each field.

Also, the fields may also come from different tables.

I DON’T want to do:

if ( isset ( $values_to_update[ "telephone" ] ) )
    $database->update("UPDATE users SET telephone = '{$values_to_update[ "telephone" ]}' WHERE user_id = $user_id");

else if ( isset ( $values_to_update[ "email_address" ] ) )
    $database->update("UPDATE authentication SET email_address = '{$values_to_update[ "email_address" ]}' WHERE user_id = $user_id");

else if ( /* ... */)
    // etc etc etc

Does anybody have any ideas of a better way I could do this?

I was thinking maybe I could use one HUGE update statement uses the same select statment that fetches the data. but i dont know how this would work.

Is it a standard practise for applications to store an array of fields to tables for dynamic query generation?

  • 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-29T08:57:02+00:00Added an answer on May 29, 2026 at 8:57 am

    A clean way I’ve found of doing this in PDO-style ORM mappers (which is also used in Zend Framework):

    public function save($model) {
        // Possible values to insert
        $values = array(
            ":id" => $model->id,
            ":name" => $model->name,
            //...
            ":contact_number" => $model->telephone
        );
    
        // If model id is set, we are updating a record, otherwise inserting
        if (isset($model->id)) {
            $sql = "UPDATE tbl SET name = :name, ..., contact_number = :contact_number WHERE id = :id LIMIT 1";
        } else {
            // Don't pass the ID when it isn't in the query
            unset($values[":id"]);
            $sql = "INSERT INTO tbl (name, ..., contact_number) VALUES (:name, ..., :contact_number)";
        }
    
        // Execute the query with our values (below is a PDO example)
        $stmt = $this->db->prepare($sql);
        $stmt->execute($values);
        $stmt->closeCursor();
    
        // Get the new ID if we inserted
        if (!isset($model->id)) {
            $model->id = $this->db->lastInsertId();
        }
    
        return $model;
    }
    

    Usually this amounts to writing a single save method for each model. You could extend this in UPDATE situations by keeping a copy of the originally-retrieved model, so that you only update the columns that have changed, however I don’t see any huge overhead by updating all columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.