简体   繁体   English

显示在SQL查询中修改的数据

[英]Display data which was modified in SQL query

I have an table 'users'. 我有一个表“用户”。 Users can modify their data eg. 用户可以修改他们的数据,例如。 name, surname, email. 姓名,姓氏,电子邮件。 and I want to send email to admin which will contain details about modified data. 并且我想发送电子邮件给管理员,其中将包含有关已修改数据的详细信息。 If user change only name and email i want to grab only changed data - in this case only name and email. 如果用户仅更改名称和电子邮件,我只想获取更改的数据-在这种情况下,仅更改名称和电子邮件。

My form: 我的表格:

<form action="" method="post">
<input type="text" name="name">
<input type="text" name="surname">
<input type="text" name="email">
<input type="submit" value="Send">
</form>

It is possible that I can grab data which was modified in sql query, excluding data which are the same in table and in the form? 有可能我可以获取在sql查询中修改过的数据,但不包括表和表格中相同的数据? I have an idea that I must check each POST field that is the same as in database, but I have many more fields and this solution is not so good: 我有一个主意,我必须检查与数据库中相同的每个POST字段,但是我有更多的字段,并且此解决方案不太好:

if($row['email'] !== $_POST['email']) { $changed_email = $_POST['email']; } 

if($row['name'] !== $_POST['name']) { $changed_name = $_POST['name']; }

There is any other solution that I can grab only modified data? 还有其他解决方案,我只能获取修改后的数据吗? I'm using Zend Framework and all table fields are called same as POST data. 我正在使用Zend Framework,所有表字段都称为POST数据。

You could loop through your $_POST array and use the key to fetch the corresponding value from the DB: 您可以遍历$_POST数组,并使用键从数据库中获取相应的值:

foreach($_POST as $key => $value){
    if($value !== $row[$key]){
        $changed[$key] = $value;
    }
}

However you may want to exclude some fields in the loop (for example the submit button): 但是,您可能要在循环中排除某些字段(例如,提交按钮):

$excluded_keys = array('submit', 'some_other_key');
foreach($_POST as $key => $value){
    if(in_array($key, $excluded_keys)) continue;

    if($value !== $row[$key]){
        $changed[$key] = $value;
    }
}

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

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