[英]Cant Getting multiple rows with same foreign key in SQL using codeigniter 3
[英]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.