Prerequisites
I use PHP mysql to make a data add modification function, about the modification function. I need to get what the data modifies every time, so that it can be stored as a log data for viewing.
Example
There is a data table with the contents of
id,名称,编号,等…
There is a data item that
1,小明,101,等…
Then I passed the update quotation, modified Xiao Ming for Xiao Red , modified 101 for 102 and other data.
At this time I would like to make a log record, save xxx To change Xiao Ming to small red, etc.
Problem
That's the problem. How to quickly and effectively determine the content of the changes.
The way I do it
Because I loaded the data into the Web page before editing the data, and then when the page was submitted, I judged whether the submitted content was the same as the data that was just loaded, and that it was changed.
I think the drawbacks
Because my data a lot of many not only the ID name of these several, so it is particularly troublesome to judge, many if, such as if name = = NewName, such as the judgment of quotations.
Demand
Is there a more efficient, more refined way to implement the code?
Reply content:
Premise
I use PHP mysql to make a data add modification function, about the modification function. I need to get what the data modifies every time, so that it can be stored as a log data for viewing.
Example
There is a data table with the contents of
id,名称,编号,等…
There is a data item that
1,小明,101,等…
Then I passed the update quotation, modified Xiao Ming for Xiao Red , modified 101 for 102 and other data.
At this time I would like to make a log record, save xxx To change Xiao Ming to small red, etc.
Problem
That's the problem. How to quickly and effectively determine the content of the changes.
The way I do it
Because I loaded the data into the Web page before editing the data, and then when the page was submitted, I judged whether the submitted content was the same as the data that was just loaded, and that it was changed.
I think the drawbacks
Because my data a lot of many not only the ID name of these several, so it is particularly troublesome to judge, many if, such as if name = = NewName, such as the judgment of quotations.
Demand
Is there a more efficient, more refined way to implement the code?
If the old and new data is represented by a one-dimensional array, the PHP function Array_diff_assoc can be used to compare two arrays.
Using a lot of if to judge is very stupid method, also do not recommend to the database to do this kind of work
The direct analysis of the MySQL log is not good.
In the case of form data, one method is to compress the multidimensional form data into a string, and then find the length (int) value of the string to be stored in the change entry. The next time you compare it, just pull up the string and compare it to the compression string that submitted the form. The same length is not changed, the length is different. Note the problem of traditional Simplified Chinese characters.
Trigger? I remember being able to do it in Oracle. We've done it. Copy the data to another table
But if the diff to field may not be realistic
Before the last time also made a need to determine which fields have not changed
I also use your approach, but there is no judgment if, that is, two arrays (new data, old data) to compare, their key values are consistent, and then directly write a method two key value corresponding to the comparison value, the difference is recorded
In fact, I think the main method may not be, through the title of the main description, it should be a large form Update
and this entrance will not be a lot of it?
I think the only thing I can do is not to put these things in the main business process and should be asynchronous. Karma can do this asynchronously through messaging mechanisms or other means.