0

この関数を codeigniter で設定して、cron ジョブとして実行し、すべてのユーザーを X 時間でログアウトします。1 人のユーザーを選択してそのユーザーをログアウトするときに、このコードを正常に使用しましたが、foreach を追加すると、次のエラーが発生します。

Error Number: 1062

Duplicate entry '3858' for key 'PRIMARY'

INSERT INTO `time` (`id`, `projectid`, `phaseid`, `firstname`, `timest`, `status`, `activityid`, `todate`, `remark`, `time`, `workperiod`, `activitydate`, `entrydate`) VALUES ('3858', '212132 Unilever Nigeria  10000L Toothpaste Storage Tanks x 2', '1', 'BONGANI', '1347610976', '1', '1', '1', '1', 3335907, 'nothing', '2012-09-14 10:22:56', '2012-10-23 01:01:23')

Filename: C:\xampp\htdocs\projectfiles\protime\v2\system\database\DB_driver.php

Line Number: 330

これはモデルのコードです:

function punch_out() {
    $this -> load -> database();
    $this -> load -> helper('date');
    //get people
    $this -> db -> select('*');
    $this -> db -> from('person');
    $this -> db -> order_by("department", "asc");
    $this -> db -> order_by("userid", "asc");
    $query = $this -> db -> get();
    foreach ($query->result_array() as $row) {
        $people[] = $row;

    }

    foreach ($people as $row) :

    $this -> db -> from('punch');
    $this -> db -> where('firstname', $row['firstname']);
    $this -> db -> order_by("id", "desc");
    $this -> db -> limit(1);
    $query = $this -> db -> get();
    foreach ($query->result_array() as $row) {
        $data = $row;
    }

    $data['workperiod'] = 'nothing';

    $current_time = getdate();

    if ($current_time['wday'] > '0' && $current_time['wday'] < '5') {
        if ($current_time['hours'] > '7' && $current_time['hours'] < '12') {
            $data['workperiod'] = 'stda';
        } else if ($current_time['0'] > strtotime('4:16am') && $current_time < strtotime('11:59pm')) {
            $data['workperiod'] = 'ota';
        }
    } else if ($current_time['wday'] == '5') {
        if ($current_time['0'] > strtotime('7:00am') && $current_time < strtotime('11:59am')) {
            $data['workperiod'] = 'stdb';
        } else if ($current_time['0'] > strtotime('1:01am') && $current_time < strtotime('11:59pm')) {
            $data['workperiod'] = 'otb';
        }
    } else if ($current_time['wday'] == '6') {
        $data['workperiod'] = 'otc';
    } else if ($current_time['wday'] == '0') {
        $data['workperiod'] = 'dtc';
    }

    if ($data['status'] == 1) {
        //MAKE TIME
        $now = time();
        $data['time'] = elapsed_time($data['timest'], $now);
        $data['entrydate'] = timestamp_to_mysqldatetime($now);
        $data['activitydate'] = timestamp_to_mysqldatetime($data['timest']);
        $this -> db -> insert('time', $data);
    }

    endforeach;
}
4

2 に答える 2

1

挿入しているのと同じIDのデータ(time、entrydate、activitydate)を更新しようとしている場合は、次のような新しい配列にデータのみを配置する方がはるかに良いと思います。

if ($data['status'] == 1) {
    //MAKE TIME
    $now = time();
    $newdata['time'] = elapsed_time($data['timest'], $now);
    $newdata['entrydate'] = timestamp_to_mysqldatetime($now);
    $newdata['activitydate'] = timestamp_to_mysqldatetime($data['timest']);
    $this -> db -> where('id',$data['id']);
    $this -> db -> update('time', $newdata);
}

または、データベースに新しいレコードを挿入しようとしている場合は、挿入しようとしている配列名を変更するだけです。

if ($data['status'] == 1) {
    //MAKE TIME
    $now = time();
    $newdata['time'] = elapsed_time($data['timest'], $now);
    $newdata['entrydate'] = timestamp_to_mysqldatetime($now);
    $newdata['activitydate'] = timestamp_to_mysqldatetime($data['timest']);
    $this -> db -> insert('time', $newdata);
}

このコーディング方法では、将来のエラーを回避し、時間を節約できる可能性があります。

乾杯!

于 2012-10-23T02:29:59.613 に答える
0

私がこれを正しく読んでいれば、そのパンチの一意の ID を$data含む、各「パンチ」に関する詳細が含まれています。からフィールドを$data['id']削除することは決してないため、ソースとしてデータベースにエントリを挿入すると、新しいエントリがプル元のパンチと同じであることも指定されます。それを 2 回実行しようとすると、同じ.$data['id']$datatime$dataidididtime

unset($data['id'])コードとデータ構造を正しく理解していれば、挿入を実行する前に解決策があると思います。

于 2012-10-22T23:23:34.037 に答える