我拥有arrays
中表中的所有数据。其中updated existing data
和一些added new data
。该数据是从 CSV
导入的,并存储在array
中。
问题是:
如何在Codeigniter中使用“ ON DUPLICATE KEY UPDATE
”通过单个查询插入和更新现有数据?
像这样的桌子。
id(auto incre) invoice_code item_code item_rate
1 INPO311018-1 pip640up 62
2 INPO311019-43 plxliupp 43
3 INPO311012-05 al6408f 24
在同一时刻插入并更新后的表将如下所示。
id(auto incre) invoice_code item_code item_rate
1 INPO311018-1 pip640up 59.99
2 INPO311019-43 plxliupp 40
3 INPO311012-05 al6408f 25.99
4 INPO011019-3 Ndry_milk 1.4
5 INPO021012-05 al894_ad 99
控制器
function import_csv()
{
$this->load->library('csvimport'); //Load Library
$file_data=$this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
foreach ($file_data as $row) {
$data[]=array(
'id'=>$row['id'],
'invoice_code'=>$row['invoice Code'],
'item_code'=>$row['item Code'],
'item_rate'=>$row['item Code'],
);
}
$this->load->model('invoice_model');
$this->invoice_model->insert_data($data);
}
型号
function insert_data($data) //Add & Update table with "CSV"
{
$this->db->insert_batch('po_invoice',$data);
}
**非常感谢,谁能解决这个问题:) **
答案 0 :(得分:1)
这太疯狂了..也许吧..我没有正确地使用它...但是尝试一下...
<script>
gtag('event', 'purchase', {
"send_to": "AW-XXXXXXXX/<Conversion Label>",
"transaction_id": "24.031608523954162",
"affiliation": "Google online store",
"value": 23.07,
"currency": "USD",
"tax": 1.24,
"shipping": 0,
"items": [
{
"id": "P12345",
"name": "Android Warhol T-Shirt",
"list_name": "Search Results",
"brand": "Google",
"category": "Apparel/T-Shirts",
"variant": "Black",
"list_position": 1,
"quantity": 2,
"price": '2.0'
},
{
"id": "P67890",
"name": "Flame challenge TShirt",
"list_name": "Search Results",
"brand": "MyBrand",
"category": "Apparel/T-Shirts",
"variant": "Red",
"list_position": 2,
"quantity": 1,
"price": '3.0'
}
]
});
</script>
处于模式...
function import_csv()
{
$this->load->model('invoice_model'); /// load it at beginning
$this->load->library('csvimport'); //Load Library
$file_data=$this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
foreach ($file_data as $row) {
$data = array(
'id'=>$row['id'],
'invoice_code'=>$row['invoice Code'],
'item_code'=>$row['item Code'],
'item_rate'=>$row['item Code'],
);
// in order to insert or update ... you cannot use insert batch ....
// pass it one by one
$this->invoice_model->insert_update_data($data);
}
// $this->invoice_model->insert_data($data);
}