Let us say that we have an SQL database with a contacts table that has 5 fields; contactID, title, firstName, middleName, and lastName. On the front end we have a profile page with an update button.
Let us also say that we want to allow the user to update any given field without having to also enter data in the other fields (a last name change, for example.). Is there a 'simple' way to allow this?
My solution is to add a bit field to each property and add a series of condition statements to generate the UPDATE statement based on the bit field.
It is ugly and seems inefficient. Any advice?
The best way is to keep data retrieved from database in all fields and update all the fields. If user changes the value, it will be updated to new value. Otherwise it will stay the same old value.
Suppose that you retrieved all the values from database and on click of edit button, they are editable(textboxes) with old value already present in them. Then write a query like
update contacts set title =@title, firstName=@firstName, middleName=@middleName, lastName=@lastName where contactID=@ContactID