如何update_batch或仅更新表中的所有行

时间:2019-03-26 05:39:39

标签: php codeigniter

我的表仅使用最后一行数据而不使用每行中的数据来更新所有行。

这是我的模特

public function update3($dtr_id)
    {   
        // $lateDdc_total_rate = $_POST['lateDdc_total_rate'];

        if (is_array($dtr_id)) 
        {
            $this->db->where_in('dtr_id', $dtr_id);
        }else{
            $this->db->where('dtr_id', $dtr_id);
        }

        $update = array(
                    'total_work_hours' => $this->input->post('total_work_hours'),
                    'time_lates' => $this->input->post('total_lates'),
                    'late_deduction' => $this->input->post('lateDdc_total'),
                    'time_under' => $this->input->post('total_under'),
                    'under_deduction' => $this->input->post('underDdc_total'),
                    'is_ACCapproved' => 1,
                    'is_computed' => 1
                    );
        $this->db->update($this->table_name, $update);
        return $update?true:false;
    }

这是我的控制器的样子

if ($this->input->post('bulk_submit3')) 
        {
            $submit = $this->input->post('checked_id');

            if (!empty($submit)) 
            {
                $submit = $this->dtr_model->update3($submit);

                if ($submit) 
                {
                    $data['statusMsg'] = 'Selected DTR Reports have been successfully Submitted.';
                }else{
                    $data['statusMsg'] = 'Some problem occurred, please Call Your IT Administrator.';
                }
            }else{
                $data['statusMsg'] = 'Select at least 1 record to be Submit.';
            }
        }

我到处搜索并且仍然没有答案

2 个答案:

答案 0 :(得分:0)

尝试使用set

对此进行更新
public function update3($dtr_id) {   
    if (is_array($dtr_id)) {
        $this->db->where_in('dtr_id', $dtr_id);
    }else{
        $this->db->where('dtr_id', $dtr_id);
    }

    $this->db->set('total_work_hours' , $this->input->post('total_work_hours'));
    $this->db->set('time_lates' , $this->input->post('total_lates'));
    $this->db->set('late_deduction' , $this->input->post('lateDdc_total'));
    $this->db->set('time_under' , $this->input->post('total_under'));
    $this->db->set('under_deduction' , $this->input->post('underDdc_total'));
    $this->db->set('is_ACCapproved', 1);
    $this->db->set('is_computed', 1);
    $this->db->update($this->table_name);

    return $this->db->update($this->table_name);
}

您也可以按照自己的方式做,但是您必须将第三个参数传递给update,在这种情况下为WHERE。

这是CI文件中update函数的定义。

// from: system\database\DB_query_builder.php @ 1821
public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)

答案 1 :(得分:0)

请在模型中编写以下代码。

light.exe

请从控制器中编写以下代码

 function update_data($table, $data = false, $other= false) {
  if ($other != false) {
        if (array_key_exists('where', $other)) {
            $this->db->where($other['where']);
        }
       if (array_key_exists('where_in', $other)) {
            $this->db->where_in($other['where_in']);
        }
     }
    $this->db->update($table, $data);       
    return $this->db->affected_rows();
}

此代码将为您提供帮助