stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,
I was wondering how one might check if an element of a dynamically generated form (for example, a form with elements populated with values from a MySQL database)that is most likely fairly large has been changed, and if so, update that elements respective record with it's data?

I've thought about doing it a couple ways:
  1. fetching the data from the db, then comparing it to the data submitted, and if a certain part of that data has been changed, update it
  2. ..or updating the entire table with the submitted data


Well, any help is appreciated. Thanks in advance.

dhoss
weeee!

Replies are listed 'Best First'.
Re: site management with perl
by Zaxo (Archbishop) on Apr 18, 2004 at 03:22 UTC

    A timestamp column is the simplest way to track updates in MySQL.

    After Compline,
    Zaxo

Re: site management with perl
by BUU (Prior) on Apr 18, 2004 at 09:08 UTC
    So you display a website generated from a database with a bunch of form elements that correspond to various fields in the database, and you want to know which, if any of the form fields have been changed so you can update the database?

    Assuming the above is correct, I seriously wouldn't bother. I doubt you'd gain any real benefit or efficiency from trying to only update the changed elements. Just update all of the fields with the form values. Any win from doing less updating is probably equally offset by the calculations involved in diffing.

    Alternatively, storing the original value in a hidden field is probably the best bet. Sure you can't rely that the client hasn't changed the value of the hidden field, but what does that gain them? If they deliberately modify the hidden one to make it the same as the visible, you simply don't update the database and it's their loss. Other wise it works properly with the least amount of fuss.
Re: site management with perl
by tilly (Archbishop) on Apr 18, 2004 at 03:52 UTC
    Has been changed where? In the form? Or in the database?

    One solution if you want to know if it was changed in the form is that for every visible form element you can store the original value in a hidden field. If the two differ, then that field was updated in the form. (I didn't say it was a good solution...)

      Or if you are using sessions, and populate the fields from the current record(s) in the database -- you can tuck the current values into the session as you generate the form. Then when it is submitted you can compare with your session stash and act. This method limits the amount queries to the DB and the amount of data passed over the GET/PUT.


      -Waswas
Re: site management with perl
by stonecolddevin (Parson) on Apr 18, 2004 at 03:25 UTC
    So compare the current timestamp with the original timestamp (for example, the creation date) and if they aren't the same, update?

    meh.
Re: site management with perl
by stonecolddevin (Parson) on Apr 18, 2004 at 04:05 UTC
    If the data was updated in the form, sorry, should have specified that.

    meh.
Re: site management with perl
by stonecolddevin (Parson) on Apr 18, 2004 at 23:56 UTC
    Ok, this may not apply right now, given I'm not going to have that much data to deal with, but if I were to update the entire table, how would speed be affected? Would retrieving from the db slow down noticeably?
    meh.