Page 1 of 1

Update only changed fields

Posted: Fri Nov 05, 2004 9:28 pm
by bimo
Alright, I'm trying to find the best way to update a db record but only the field(s) that has(have) changed.

each record has nine different elements which the user enters on one page, which is how everything gets into the database in the first place. If the user calls up the records later from a certain page, some of the form fields are populated by returned record elements. The user then has the choice to update the form field and change the record or records.

I am trying to find the easiest way to have it figure out if the form field(s) is(are) different from the current values in the record(s). I have thought that I could have it compare every single form field and if they're different have it update but thought there might be an easier way.

How about if I save the $row[] array elements somehow (in a $_SESSION, I guess) and compare old results with the new $_POST? Thing about that that makes me pause is that it will probably be a two dimensional array and I don't know how bad it would be to fill up $_SESSION with data I don't even know if I'll be using.

Also, each form element (and therefore each record element) has it's own submit button because I was thinking that I could identify which submit button was pushed but wouldn't I then either need to use javascript or give each form element its own form?

Does any of that sound doable? Maybe someone has a cleaner idea?

Thanks

Posted: Sat Nov 06, 2004 12:15 am
by harsha
SESSIONS should be used to store data like user ID, login no(as they are global to entire site). etc, it is a bad Idea to store the form data in session, and this is my idea, because say you have open the same page twice in the same browser and you are entering different data in each page and when you submit, there is every chance of data being overwritten on the other page. since session is browser dependent. both the forms will be using same session so same data shared by two form because of session.

Posted: Sat Nov 06, 2004 3:24 am
by swdev
Personally, I would say that if the user has pressed the 'submit' button, then they want that data to be updated, even if they haven't changed anything, so I would always update the database.

However, if you did want to know whether the data has been changed, then I would generate a hash value from when the data is read in, and when the user presses the submit button, generat another has of the data in the form. Compare the two hash values. If they are different, then some data has changed, so you know to update the datbase.

Hope this helps

Posted: Sat Nov 06, 2004 9:53 am
by bimo
Thanks for the sessions advice. I've just started with them and don't know all of the ins and outs yet.

so do hash values have to be passed by reference? I've never used references to pass values in PHP. is it similar to C++? (not that I even remember much C++.)

Is that just another way of saying, "query the db, and compare the results with the post array?"

Thanks for your help.

Posted: Sat Nov 06, 2004 10:07 am
by swdev
With regard to hash values, what I was meaning was
  1. When you read the data from the database to populate the form create a hash value. This could be as simple as just concatenating all the fields into a single string.
  2. Store this value in a hidden field on the form.
  3. When you post the form, perform the same hash function on the post variables, remembering not to include the hidden hash value.
  4. If the hash value you have calculated matches the one in the hidden field on the form, then don't update the database.
What this does is save a trip to the database if the fields haven't changed.

Hope this makes things clearer

Posted: Sat Nov 06, 2004 10:14 am
by bimo
Great. Thanks

Posted: Sat Nov 06, 2004 10:49 am
by bimo
One last thing: is there no way that I can have the specific post button send it's identity so that I don't have to compare every field with the db?