简体   繁体   中英

Best way to make changes of database records trackable?

I got a PHP webapp with an HTML frontend for users to make changes in a MySQL database table. I also made an archive table for old records to move them into when a user changes a record.

But I don't only want to save old records in the archive table, I also want to obtain which columns have been changed in particular.

Due to the nature of web-based database frontends I can't easily obtain which columns have been changed as every time a user opens a record the script serves the contents of the row to pass them into the value fields of the HTML input elements in my frontend (or select , textarea , etc.).

When the users changed their values in the input fields and submit the data the browser doesn't only send the altered fields but all the data that has been served to the frontend in the first place including the values changed by the user. Thus I also have to update the row even with unaltered values.

Original data   Passed to frontend  Changes by user      Data being sent back
  id=827
val1=arthur     val1=arthur                              val1=arthur
val2=ford       val2=ford                                val2=ford
val3=tricia     val3=tricia         val3=trillian        val3=trillian
val4=zaphod     val4=zaphod                              val4=zaphod
val5=marvin     val5=marvin                              val5=marvin
   .
   .
   .

In the example above after submitting the data the backend would simply make a copy of the current record in the archive table and update the 'live' record with the values of val1 to val5 .

Now I'd like to make a Last changes pages on which you can see what information has been changes, for example:

2014-04-08: User 'douglas' changed val3 in row #827 

or…

2014-04-08: User 'eoin' changed val1, val2 and val3 in row #137 

At the moment I see two possible solutions:

  • Comparing the current row with the corresponding row in the archive table to get the names of the columns (!) in which the rows differ. Is there a MySQL function to do this? Would I have to implement this in PHP?

  • In the PHP backend after receiving the data from the frontend immediately check which values differs from the original record to store only the altered values. So I would automatically have the columns in question.

On subimt, you have the updated/"or maybe not" fields and you have the original values, right? Both data sets might be represented like this:

$updated_data = ['first_name' => 'John', 'last_name' => 'White'];
$original_data = ['first_name' => 'Peter', 'last_name' => 'White'];

Now you could use array_diff_assoc() and array_keys() to find the modified fields:

$updated_fields = array_keys(array_diff_assoc($updated_data, $original_data));

print_r($updated_fields);  # List of updated fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM