Is there a way to show which field you updated in mysql?

When users edit their account, I want to display a confirmation page displaying their updated information, with the fields that they have changed in bold. Is there a mysql statement that allows you to select the fields that the user changes? If not, how exactly can I achieve this.

Example:

(editaccount.php)
first_name: bob
last_name:  builder
email: bobbuilder@gmail.com

When they change, say your name in "james", the confirmation page shows your first name in bold because they changed it, but other areas are still normal text.

first_name: <b>James</b>
last_name: builder
email: bobbuilder@gmail.com
+3
source share
3 answers

You need to compare the fields with the old data yourself.

very simple example:

$old_user_data = <fetched from db>;
$new = $_POST;
$changed = array();

foreach ($old_user_data AS $field => $value) {
  if ($new[$field] != $value) {
     $changed[] = $field;
  }
}

print_r($changed); // array contains all fields that were changed
+3

, php. , .

+3

I don't know if MySQL can tell you which fields have been changed. I would like to hear, though. In PHP, you can simply compare the information before and after the update.

// select current info, store it in $before, e.g.
// then go through the posted values
$changed = array();

foreach ($_POST as $key => $value) {
  // compare new value against the previous one
  if ($before[$key] != $value) {
    $changed[] = $key;
  }
}
+2
source

Source: https://habr.com/ru/post/1746093/


All Articles