每当我尝试插入数据时,两列的字段都为空。只有自动递增(当然)MainReqID和最后一列有字段。 这是我的控制器..
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..
答案 0 :(得分:1)
看起来$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。
答案 1 :(得分:0)
试用此代码:
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);
}