php - 自动将新条目添加到表 codeigniter 中

标签 php mysql database codeigniter

我正在制作一个程序,根据每位员工的出勤情况计算膳食津贴。这是我现在拥有的数据,一个名为 attendance 的表。该表是从我导入的 csv 文件填充的,这是我导入 csv 的代码

function uploadData()
{
    $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
    // Ignore header line
    $header = fgetcsv($fp);
    // Array to store the partial records
    $attendance= [];
    while($csv_line = fgetcsv($fp))
    {
        // Key to identify first part of record (Name and date)
        $key = $csv_line[1]."/".$csv_line[3];
        if ( isset($attendance[$key]) ){
            $start = $attendance[$key];
            // Extract data from first part of record and add in end date from current row
            $data = array(
                'id_attend' => $start[0] ,
                'emp_code' => $start[1],
                'emp_name' => $start[2],
                'date' => $start[3],
                'time_in' => $start[4],
                'time_out' => $csv_line[4],
            );
            $data['crane_features']=$this->db->insert('attendance', $data);
            // Remove partial record
            unset($attendance[$key]);
        }
        else    {
            // Store partial record
            $attendance[$key] = $csv_line;
        }
    }
    fclose($fp) or die("can't close file");
    $data['success']="success";

}

这是我的 table


|   id_attend   |   emp_code   |   emp_name   |   date   |  time_in |  time_out  |
----------------------------------------------------------------------------------
|   0001        |   brw        |   brown      |01.01.2001|  07.00   |    20.00   |
|   0002        |   cny        |   cony       |01.01.2001|  07.00   |    20.00   |
----------------------------------------------------------------------------------

我需要计算他们参加的天数并将其保存到我的数据库中。我已经有一个名为 allowance

的表作为容器
------------------------------------------------------------------------
|id_allowance(auto increment)|emp_code|emp_name|days_attended|allowance|
------------------------------------------------------------------------
|          0001              |   brw  |  brown |       1     |  30.00  |
|          0002              |   cny  |  cony  |       1     |  30.00  |
------------------------------------------------------------------------

有什么方法可以在每次导入新的 csv 文件时自动生成和更新津贴?因为津贴表本来就应该是空的,而且实际上取决于出勤表

最佳答案

为此,您需要在 MySQL 中创建一个触发器,以便当新数据插入到 attendance 表中时,同一查询将自动生成并针对 allowance 表运行.

DROP TRIGGER IF EXISTS allowance_insert_trigger;
DELIMITER $$
 CREATE TRIGGER allowance_insert_trigger
 AFTER INSERT ON attendance FOR EACH ROW
 begin
   DECLARE days_attandence varchar(10);
   SELECT count(*) INTO days_attandence FROM attendance WHERE emp_code=NEW.emp_code GROUP BY date;
   INSERT INTO `allowance` ( `emp_code`, `emp_name`, `days_attended`, `allowance`) VALUES ( NEW.emp_code, NEW.emp_name, days_attandence, '30.00');
 END;
$$
DELIMITER ;

您想像这样编写触发器,在这里我已经为容差设置了静态值,但您可以在那里放置您自己的计算值。

关于php - 自动将新条目添加到表 codeigniter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51676733/

相关文章:

php - $_GET 变量是查询在传递时不起作用

mysql - 数据库表中的行限制

php - Facebook 如何通知并立即显示新评论,或者 Stackoverflow 是如何做到的?

php - 使用 php LOAD DATA INFILE 将 CSV 文件导入 MySQL 表

php - PHP 中的 Codeigniter 和 Mysql 程序

php - 删除句子中的所有空格和逗号

php - 如何使用 phpmailer 跟踪通过 smtp 发送的电子邮件?

php - 连接 MySQL 中条件不为 null 的表

mysql - 如何更正 SQL 表中的日期值?

sql-server - 从 SQL Server 中的多个表中删除多条记录的最佳实践