【问题标题】:is there a way to show what field you updated in mysql?有没有办法显示你在 mysql 中更新了哪些字段?
【发布时间】:2010-05-18 22:50:24
【问题描述】:

当用户编辑他们的帐户时,我想显示一个确认页面,其中显示他们的更新信息以及他们更改的字段以粗体显示。是否有 mysql 语句允许您选择用户更改的字段?如果没有,我该如何实现这一目标

例如:

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

当他们将名字改为“james”时,确认页面 以粗体显示他们的名字,因为他们改变了名字,但其他区域仍然是普通文本。

first_name: <b>James</b>
last_name: builder
email: bobbuilder@gmail.com

【问题讨论】:

  • 假设您要提交此表单,您只需将 $_POST vars 与 MySQL 中的原始数据进行比较,并在值不匹配时调整显示。

标签: php mysql


【解决方案1】:

您需要自己将字段与旧数据进行比较。

非常基本的例子:

$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

【讨论】:

    【解决方案2】:

    我会在应用程序级别处理它,在 php.ini 中。在提交更新之前,只需从数据库中提取数据,将每个字段与提交的数据进行比较,并根据自己的喜好处理每个不等式。

    【讨论】:

      【解决方案3】:

      我不知道 MySQL 是否可以告诉您哪些字段被更改了。有兴趣听听如何。在 PHP 中,您可以简单地比较更新前后的信息。

      // 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;
        }
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-20
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 2015-10-19
        • 1970-01-01
        相关资源
        最近更新 更多