Codeigniter从表中克隆数据并更新它(使用数组)

时间:2019-07-04 03:52:23

标签: php codeigniter codeigniter-3

尝试从表“付款”到“付款历史”克隆数据(更新)

付款表

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

付款历史记录表

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

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

我的代码: 控制器

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
    }

}

表数据

Payment               Payment History
+-----+---------+    +------+------------+---------+
| ID  | remark  |    | ID   |remark      | amount  |
+=====+=========+    +======+============+=========+
|  1  |100 done |    |   1  |  100 done  | 100     |
+-----+---------+    +------+------------+---------+
|  2  |200 done |    |   1  |  200 done  | 200     |
+-----+---------+    +------+------------+---------+
                     |  2  |  500 done   | 500     |
                     +------+------------+---------+

尝试使用数组克隆数据 上面是我的桌子的示例

1 个答案:

答案 0 :(得分:0)

假设“付款表”名称为payment,并且“付款历史”表名称为payment_history,您可以尝试如下操作:

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('payment');
        $result = $query->row_array();

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

}