CI 框架批量添加数据(如果数据库有就更新数据)
model:
public function insert_select($values) { $sql = ‘INSERT INTO ‘ . $this->_table_name . ‘(article_id,user_id,share_link,short_link,read_price,created_at,updated_at) VALUES ‘ . $values . ‘ ON DUPLICATE KEY UPDATE article_id=values(article_id),user_id=values(user_id),share_link=values(share_link), read_price=values(read_price),created_at=values(created_at),updated_at=values(updated_at)‘; $query = $this->db->query($sql); return $query; }
controllers:
批量添加的数据$res数组是:
//拼接sql $sql = ‘‘; foreach ($res as $key => $val) { $sql .= ‘(‘ . $val[‘article_id‘] . ‘,‘ . $val[‘user_id‘] . ‘,‘ ."‘". $val[‘share_link‘] ."‘".‘,‘ ."‘". $val[‘short_link‘] ."‘". ‘,‘ . $val[‘read_price‘] . ‘,‘."‘" . $val[‘created_at‘] ."‘". ‘,‘."‘" . $val[‘updated_at‘] ."‘" . ‘)‘.(($key)+1==count($res)?‘‘:‘,‘); } $arr = $this->share_record_model->insert_select($sql);
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30