>我有一个包含三个字段的表格
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++;
}
您可以创建一个新表,例如"组"字段:id(自动增量)和group_name(varchar)随机生成组名。
然后在插入产品创建一个新组之前,您将获得一个唯一的新组 ID。
然后使用该组 ID,您可以继续您的选择