简体   繁体   English

使数据库记录的更改可跟踪的最佳方法?

[英]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. 我有一个带有HTML前端的PHP Webapp,供用户在MySQL数据库表中进行更改。 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.). 由于基于Web的数据库前端的性质,当用户每次打开记录时,我无法轻易获得更改了哪些列的脚本,脚本将为行的内容提供服务,以将其传递至HTML input元素的value字段中。我的前端(或selecttextarea等)。

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. 当用户在input字段中更改其值并提交数据时,浏览器不仅会发送更改后的字段,还会首先发送所有已提供给前端的数据,包括用户更改的值。 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 . 在上面的示例中,提交数据后,后端将简单地在存档表中复制当前记录,并使用val1val5的值更新“活动”记录。

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? 有MySQL函数可以执行此操作吗? Would I have to implement this in PHP? 我需要在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. 在PHP后端中,从前端接收到数据后,立即检查哪些值与原始记录不同,以仅存储更改后的值。 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? 在subimt上,您具有“更新” /“或也许没有”字段,并且您具有原始值,对吗? 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: 现在,您可以使用array_diff_assoc()和array_keys()查找修改后的字段:

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

print_r($updated_fields);  # List of updated fields

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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