A question : updating multiple fields in a database table

Hello there everyone.

Hope you’re doing well and staying safe.

A little description here :wink:

There’s a database table USER_DETAILS with ID, NAME, EMAIL_ID, as fields. And a basic HTML form connecting to USER_DETAILS database table.

Screenshot 2021-06-28 at 23-24-33  form - HTML HyperText Markup Language MDN
A basic HTML page.
(source : MDN Mozilla)

Note: System means program or software here. And the user is NOT accessing the database directly with a terminal or a GUI.

The system allows the end user to enter the details and modify/update them.

Let’s say there are more than two fields, probably 10 — address, zip code, contact etc. And the user can update the details.

My question is related to the code.

  1. If the system allows user to update the same exactly one field every time, the code is easy. Write update query updating the particular field and voila.
  1. Say the system only allows to update all the details every time. (Practically, if a user is updating all the details every time, it means the details are either invalid or totally updated. So, let’s not digress any further here). It’s same as the above case write update query with all the fields.
  1. What if the user wants to update only a few fields? Like EMAIL ID, or Address or both with zip code. The fields won’t be the same to all the users every time. This makes the code a bit tough and interesting. There are two possibilities I can think of.
  • One, write a switch or if statement to check the field user is updating and write update query.

It’d look like this
if field == 'NAME' update_name() //calls a function to update the name in the database
if field == 'EMAIL_ID' update_email_id() //calls a function to update the email id in the database

But there’s a problem with this approach. If there are more than two fields, the program access the database multiple times for each field. It’s like a single update query is sub divided into multiple queries. I think it’s not a good approach. I’m not sure why and I’d be glad if you let me know.

  • Second approach is retrieve the preexisting details of the user and compare it with the want-to-update details. If the want-to-update details are same to preexisting data or NULL — meaning the user do not want to update the details — keep the preexisting details, else replace and finally write the update query with all these new values.

Is this a good approach to update the details? Retrieving, comparing and updating. Because the unchanged values are set to the same values. So they remain same before and after the query. I don’t think it’s a good approach.

These are the only possible ways I can think of to update fields in the database table.

I want you to tell me your opinion(s) on these approaches to updating the values and also the other approaches if you know. Anything.

Thanks in advance.

Have a great time.


I don’t how to ask this in a single question. Any idea how to ask or explain this to someone? What is the terminology?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.