how to only bulk update rows of form data that were modified

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

how to only bulk update rows of form data that were modified

Post by sparrrow »

I am trying to replicate functionality similar to what I've seen in PHPMyAdmin. When updating/editing table data, you have a bunch of form fields...one for each column. If you only change one value, the update statement that is submitted only updates that one row or field.

Basically, I have a table of products and I print them to a table of arrayed form fields. This is so I can tab through and make updates to all my product data and submit it all at once. When I submit, I iterate through the arrays and run update statements on every row. It's started to get bogged down now that I have hundreds of products. I would like to be able to change one value of one product, click "bulk update", and it only update the one record, instead of updating the other 100 records with the same data that was already in the database.

One idea I have is to dump the original values into a set of arrayed hidden input fields, and run a check against it, but it just feels like it would be cluttered.

Code: Select all

if ($oldfield1hiddenarray[$i] != $newfield1txtarray[$i] OR $oldfield2hiddenarray[$i] != $newfield2txtarray[$i] ETC....) {
$Query = "Update BLAH BLAH"; //Update this products data because something was modified
}
I'm also worried it will double the size of my POST and possibly make performance even worse.

Open to any suggestions.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to only bulk update rows of form data that were modified

Post by Eran »

There are two ways I would consider handling this:
1. On submit, retrieve the current data from the database and arrange it and the posted data in a way that they could be compared easily (using array_diff() for example), and update only the differences.
2. Using javascript you could to compare each form item value against its defaultValue (the value it had when the page was loaded) when the form is submitted, and post only the values that changed.
Post Reply