php - 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 |
+------+------------+---------+
尝试使用上面的数组克隆数据是我的表的示例
解决方案
假设 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
}
}
推荐阅读
- r - 如何在 R Shiny 中替换数据表的值
- jquery - 将 XML 数据传递给 MVC 控制器
- javascript - 将不带“T”和“Z”的日期转换为带 TZ 格式的日期
- excel - 带有 IF 的 Excel 数据验证(列表)包含文本
- c++ - 调试符号稳定性
- android-studio - 如何在 ARcore 中将可变形节点添加到 OpenGL
- sql-server - 如何给datagridview空白空间的最后一行自动递增列
- php - 按数组排序给出空数组 laravel
- html - 为什么我不能在我的 flexbox 中定位 flex 项目?
- vb.net - SSRS 报告 - 未正确使用自定义代码导出到 Excel