swazerz 2019-12-04
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);