繁体   English   中英

使用Codeigniter对多个行使用相同的ID

[英]Using same id for multiple rows using codeigniter

我有一张桌子,上面有三个字段

id (primary key / auto incremented)
product_name
group_id

我的问题是,当我通过表单插入多行时,整个行组应具有相同的groupId,并且在提交时应将其递增1,因为可能有许多用户同时提交表单。 我不知道该怎么做。 请帮忙。

我的模特

function get_last_group_id() {
            $this->db->select('group_id');
            $this->db->from('mytable');
            $this->db->order_by('group_id', 'DESC');
            $this->db->limit('1');
            $query = $this->db->get();
            return $query->result();
          }

          function save_rows($ids,$product_names,$group_ids){
            $this->db->trans_begin();
            $ndx=0;
            foreach($ids as $id){

            $data = array(
            'id' => $id,
            'product_name' => $product_names[$ndx],
            'group_id' =>$group_ids[$ndx],
            $this->db->insert("product_details",$data);

            $this->db->update($this->table);
            $ndx++;
            }

即使是自动递增的,也无需为获取组ID和设置ID字段设置单独的功能

function save_rows($ids,$product_names){
                    $this->db->trans_begin();
                    $ndx=0;
        $group_id = $this->db->select("MAX(group_id) as group_id")
        ->from("mytable")
        ->get()->row_array();

                    foreach($ids as $id){

                    $data = array(
                    'product_name' => $product_names[$ndx],
                    'group_id' =>$group_id['group_id']+1,
    );
                    $this->db->insert("product_details",$data);

                    $ndx++;
                    }

您可以创建一个新表,例如“ groups”字段:id(Auto Increment)和group_name(varchar)随机生成组名。

然后,在插入产品之前,创建一个新的组,然后您将获得一个唯一的新组ID。

然后使用该groupID可以继续进行选择

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM