Codeigniter从表克隆数据并更新它

时间:2019-07-03 05:57:56

标签: php codeigniter codeigniter-3

我要做什么:
尝试从表“付款”到“付款历史”中克隆数据(更新)

付款表

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

付款历史记录表

Payment_ID
PaymentHistory_ID
Payment_Amount
Payment_Method
Payment_Remark

两者具有相同的列和相同的数据类型

我的代码: 控制器

    public function updateHistory($Payment_ID){

  $this->db->select('*'); 
  $this->db->from('Payment');   
  $this->db->where('Payment_ID', $Payment_ID); 
$query = $this->db->get();
    $this->db->update('PaymentHistory',$query);

}

1 个答案:

答案 0 :(得分:0)

您可以这样做:

$query = $this->db->where('Payment_ID', $Payment_ID)->get('Payment');
foreach ($query->result() as $row) {      
      $this->db->where('Payment_ID', $row->Payment_ID)->update('PaymentHistory', $row); // To update existing record
      //$this->db->insert('PaymentHistory',$row); // To insert new record
}