Codeigniter-克隆数据(但请删除1个不存在的列)

时间:2019-07-03 08:30:09

标签: php codeigniter codeigniter-3

我要做什么:
尝试将数据从“付款历史”表克隆到“付款”,但是现在的问题是付款历史记录表中有“ PaymentHistory_ID”列,该如何忽略呢?

付款表

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

付款历史记录表

Payment_ID
PaymentHistory_ID
Payment_Amount
Payment_Method
Payment_Remark

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

我的代码: 控制器

        public function updateHistory($Payment_ID){

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

    }

基于我提供的问题 Codeigniter Clone Data from table and update it ,代码有效,但不适用于我面临的新问题。

注意:CodeIgniter的新功能

1 个答案:

答案 0 :(得分:0)

具有单个Payment_ID

public function updateHistory($Payment_ID){
        // with single Payment_ID

        //get data with specific column and ignore PaymentHistory_ID
        $query = $this->db->select('Payment_Amount','Payment_Method','Payment_Remark')->where('Payment_ID', $Payment_ID)->get('YOUR FROM TABLE');
        $result = $query->row_array();

        if($result)
        { // check if has data
            //then update to another table
            $this->db->where('Payment_ID', $Payment_ID)->update('YOUR TO TABLE', $result ); // To update existing record
        }

}