is there a way to show what field you updated in mysql?
When users edit their account, I want to display a confirmation page that shows their updated information with the fields that they changed in bold. Is there a mysql statement that allows you to select the fields that a user chan开发者_运维问答ges? If not, how exactly can I achieve this
Ex:
(editaccount.php)
first_name: bob
last_name: builder
email: bobbuilder@gmail.com
When they change say their first name to "james", the confirmation page shows their first name in bold because they changed that but the other areas are still normal text.
first_name: <b>James</b>
last_name: builder
email: bobbuilder@gmail.com
You need to compare the fields against the old data yourself.
very basic 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
I would handle it at the application level, in php. Before committing the update, simply pull the data from the database, compare each field with the submitted data, and handle each inequality to your liking.
I don't know if MySQL can tell you which fields were altered. Interested to hear how 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;
}
}
精彩评论