每当我尝试插入数据时,两列的字段均为空。只有自动递增(当然)MainReqID 和最后一列才有字段。 这是我的 Controller ..
public function insert_main($data,$orgs){
$this->db->insert('insert_main',$data);
$getID = $this->db->insert_id();
$ctr=1;
$insertorg = array();
foreach($i=0; $i<count($orgs); $i++){
$insertorg[] = array(
'OrgID'=>$ctr[$i],
'MainID'=>$getID[$i],
'Level'=>'1234'[$i]
);
$ctr++;
}
$this->db->insert_batch('insert_mainreq',$insertorg);
}
这是它在数据库中的样子...
MainReqID | OrgID | MainID | Level
1 | null | null | 1234
2 | null | null | 1234
3 | null | null | 1234
4 | null | null | 1234
5 | null | null | 1234.. and so on..
我需要这样的东西..
MainReqID | OrgID | MainID | Level
1 | 1 | 3 | 1234
2 | 2 | 3 | 1234
3 | 3 | 3 | 1234
4 | 4 | 3 | 1234
5 | 5 | 3 | 1234.. and so on..
最佳答案
看起来$getID
不是一个数组,但您正在添加$getID[i]
。这肯定行不通。与 $ctr
相同。这是一个整数,但您正在尝试 $ctr[i]
。同样的事情也发生在 Level
上。
public function insert_main($data,$orgs){
$this->db->insert('insert_main',$data);
**$getID** = $this->db->insert_id();
**$ctr=1;**
$insertorg = array();
foreach($i=0; $i<count($orgs); $i++){
$insertorg[] array(
'OrgID'=>**$ctr[$i]**,
'MainID'=>**$getID[$i]**,
'Level'=>**'1234'[$i]**
);
$ctr++;
}
$this->db->insert_batch('insert_mainreq',$insertorg);
}
你可以尝试这个,我不确定你想用 OrgId 和 MainID 做什么,但是:
public function insert_main($data,$orgs){
$this->db->insert('insert_main',$data);
$getID = $this->db->insert_id();
$insertorg = array();
foreach($i=0; $i<count($orgs); $i++){
$insertorg[] array(
'OrgID'=> $i,
'MainID'=>$getID,
'Level'=>'1234'
);
}
$this->db->insert_batch('insert_mainreq',$insertorg);
}
请记住,如果有多行,$this->db->insert_id();
将返回插入的最后一行的 ID。
关于php - Code Igniter-批量插入功能在数据库中插入空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46029556/