简体   繁体   中英

Codeigniter Clone Data from table and update it (with Array)

Try to Clone Data (Update) from Table "Payment" to "Payment History"

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

Payment_ID
Payment_Amount
Payment_Method
Payment_Remark

Both have the same column and same data type

My Code:

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

Trying to clone data with array Above is example for my table

Assuming the Payment table name is payment and the Payment History table name is payment_history , you could try it like below :

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
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM