Hello there everyone.
Hope you’re doing well and staying safe.
A little description here
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.
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.
- 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.
- 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.
- What if the user wants to update only a few fields? Like
EMAIL ID
, orAddress
or both withzip 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 theupdate
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.
PS
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?