【问题标题】:Codeigniter Error Duplicate entry '' for key 'PRIMARY' when looping insert statement循环插入语句时,Codeigniter Error Duplicate entry '' for key 'PRIMARY'
【发布时间】:2012-10-22 23:15:56
【问题描述】:

我在 codeigniter 中将此功能设置为作为 cron 作业运行并在 X 时间注销所有用户。我在选择一个用户并将该用户注销时成功使用了此代码,但是在添加 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;
}

【问题讨论】:

    标签: codeigniter activerecord insert foreach


    【解决方案1】:

    如果您尝试更新您插入的同一 ID 的数据(时间、入口日期、活动日期),我认为如果您只将数据放入一个新数组中会更好:

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

    采用这种编码方式,可以避免以后出现更多错误,也可以节省时间。

    干杯!

    【讨论】:

      【解决方案2】:

      如果我没看错的话,$data 包含有关每个“拳”的详细信息,包括$data['id'],即该拳的唯一 ID。由于您从未从$data 中删除$data['id'] 字段,因此当您将一个条目插入到您的time 数据库中并以$data 作为源时,您还指定新条目具有相同的id你要从中抽出的拳头。一旦您尝试运行两次,您将插入具有相同id 的两行,并且从您的错误看来,您将time 表上的id 字段设置为PRIMARY - 这意味着它可以'没有任何重复。

      我认为,如果我正确理解您的代码和数据结构,解决方案是在运行插入之前unset($data['id'])

      【讨论】:

        猜你喜欢
        • 2013-09-09
        • 2018-12-01
        • 2021-10-07
        • 2019-10-11
        • 2015-10-23
        • 2021-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多