首页 > 解决方案 > Codeigniter 从表中克隆数据并更新它(使用数组)

问题描述

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

付款表

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     |
                     +------+------------+---------+

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

标签: phpcodeignitercodeigniter-3

解决方案


假设 Payment 表名称是payment并且 Payment History 表名称是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
        }

}

推荐阅读