为数据库中的任何更改创建日志

时间:2018-03-31 23:42:13

标签: php

我有一个学校系统,系统中有3个管理员。  所以我想为系统中的每个操作制作一个日志。

例如,如果有人添加了一名学生,我想看看是谁创造的。

所以到目前为止我尝试过:

<?php
$auth = $_SESSION['username'];
$query = "SELECT c_id,student_name,auth FROM school WHERE auth = '{$auth}'";
$send_q = mysqli_query($connection,$query);
if(!$send_q){


  die('rip'.mysqli_error($connection));
}

while($row = mysqli_fetch_assoc($send_q)){

$c_id= $row['c_id'];
$student_name= $row['student_name'];

echo "<div class='font-w600'>$auth added $student_name</div>"."<BR>";



}


 ?>

如果我删除该学生,该文本也将被删除。

  

John添加了Alan

如何才能删除文本呢?还有更好的方法吗?

1 个答案:

答案 0 :(得分:-1)

最简单的解决方案是写入日志文件:

<?php
$auth = $_SESSION['username'];
$query = "SELECT c_id,student_name,auth FROM school WHERE auth = '{$auth}'";
$send_q = mysqli_query($connection,$query);
if(!$send_q){
  die('rip'.mysqli_error($connection));
}

while($row = mysqli_fetch_assoc($send_q)){
    $c_id= $row['c_id'];
    $student_name= $row['student_name'];

    echo "<div class='font-w600'>$auth added $student_name</div>"."<BR>";

    # a+ means append mode
    # if you don't want others to read the log
    # make sure its outside the webserver's document root
    $logFile = fopen('path/to/log', 'a+');

    # Lock file (to prevent parallel writes)
    flock($logFile, LOCK_EX);

    fwrite($logFile, "$auth added $student_name".PHP_EOL);

    flock($logFile, LOCK_UN);

    fclose($logFile);
}

 ?>

如果您想要复杂的日志记录,您可能希望将其存储在数据库中。