I have an HTML table full of text fields. I have the ability to add new rows in the table (I am just doing this with a JavaScript function call and inserting them directly) on the fly. I need to be able to save the table values right into a (MYSQL through PHP) database. I have got the database all hooked up, but I have a question about saving the changes to my table.
How can I know which fields of the potentially dozens of fields are ‘dirty’? I am thinking it would be easiest to just clear out the database when I need to save and insert all of the data from the HTML table into the database. Is that an acceptable approach?
In general it’s better to clear out all relevant records and re-add them. It saves a lot of messing around BUT do be sure the table you’re saving contains all the relevant data. If you have state data that wouldn’t normally be editable (like creationDate you should be sure it’s available to resave.
You should also wrap the whole process in a transaction so you can roll back any deletions if saving the new data fails for any reason.