以下代码该咋写才能去重,多选插入数据,如数据库已有此数据,则自动删除

黎歌

public function badst(){
$this->title='食品信息';
$id = input('id');
$res=explode(',', $id);
$result =db('food_badst')->field('food_id')->select();

    if($id){
        foreach ($res as $k=>$v){
                $arr = [['food_id'=>$v]];
                $ids = db('food_badst')->insertAll($arr);
        }
        if($ids){
            $this->success('添加成功',true);
        }else{
            $this->error('添加失败',false);
             }
    }else{
        $this->error('失败',false);
    }
}
1380 1 0
1个回答

小七他哥

关键条件加个判断不就是了

  • 暂无评论
年代过于久远,无法发表回答
🔝