检索插入的id Codeigniter



嘿,伙计们,我需要一些帮助。我只想检索我的member_id,并将其传递到我尝试使用$this->db->insert_id()的另一个表中;但是它返回到0。但当我在表中查找时,ID值是8。我怎样才能在另一张表中得到8的值?因为我试图在一次执行中运行两个查询。这是我的示例代码。

$member = array(
'member_id' => null, 
'account_type' => $this->input->post('mem_type'),
'username' => strtolower($this->input->post('username')),
'account_type' => $this->input->post('mem_type'),
'lastname' => ucwords($this->input->post('lastname')),
'firstname' => ucwords($this->input->post('firstname')),
'middlename' => ucwords($this->input->post('middlename')),
'gender' => $this->input->post('gender'),
'email' => strtolower($this->input->post('email')),
'mobile_number' => $this->input->post('contact'),
'password' => md5($this->input->post('password')),
'upline' => $this->input->post('referral'),
'account_created' => date('Y-m-d h:i:s')
);
$member_insert_id = $this->db->insert_id();
$id = $this->input->post('referral');
//count the selected id in upline
$this->db->like('upline',$id);
$this->db->from('member');
$query =  1 + $this->db->count_all_results();   
$this->member = 0;
if($query < $this->reglvl1){
$this->member = 1;
}
if($query < $this->reglvl2){
$this->member = 2;
}
if($query < $this->reglvl3){
$this->member = 3;
}
if($query < $this->reglvl4){
$this->member = 4;
}
if($query < $this->reglvl5){
$this->member = 5;
}
$insert_downline = array(
'down_id' => null,
'level' => $this->member,
'fkmember' => $id, //referral id
'downline' => $member_insert_id //member id
);
return $this->db->insert('downline',$insert_downline);
return $this->db->insert('member',$member); 

我的另一个问题是当我在表中插入数据时。它只插入了第二个表中。这是我的表格结构:

CREATE TABLE `member` (
`member_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`account_type` ENUM('REGULAR','DUPLICATE','CORPORATE','ADVANCE') NOT NULL,
`username` VARCHAR(30) NOT NULL,
`lastname` VARCHAR(50) NOT NULL,
`firstname` VARCHAR(50) NOT NULL,
`middlename` VARCHAR(50) NOT NULL,
`gender` ENUM('Male','Female') NOT NULL,
`email` VARCHAR(50) NOT NULL,
`mobile_number` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`upline` VARCHAR(10) NOT NULL,
`account_created` DATETIME NOT NULL,
PRIMARY KEY (`member_id`),
UNIQUE INDEX `username` (`username`),
UNIQUE INDEX `mobile_number` (`mobile_number`),
UNIQUE INDEX `email` (`email`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;
CREATE TABLE `downline` (
`down_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`level` INT(10) UNSIGNED NOT NULL,
`fkmember` INT(10) UNSIGNED NOT NULL,
`downline` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`down_id`),
INDEX `fkmember` (`fkmember`),
CONSTRAINT `downline_ibfk_1` FOREIGN KEY (`fkmember`) REFERENCES `member` (`member_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;

我想先插入该成员。如果成员没有引用id,它将自动添加到成员中,并在上行链路字段中插入值"none"。引用id来自那些引用该使用以拥有成员资格的人。如果成员有引用id,它也会添加到成员表和下线表中。但是下线表将验证用户的级别。我在找水平方面没有问题。我的问题在我的下线栏中。它总是存储来自$this->db->inser_id()的值0。如何获得正确的值?伙计们,请帮帮我。

我认为$this->db->insert('member',$member);调用可能应该放在更靠上的位置,因为您可以尝试读取最后插入的ID并在downline表中使用它!否则,当您调用$this->db->insert_id()时,实际上还没有插入任何内容,因此没有什么可读取的!

您的代码应该是

编辑:

$member_insert_id = $this->db->insert_id();

这只会在对数据库进行查询后立即返回id。我看不出您在$member_insert_id = $this->db->insert_id();之前在成员表中插入了哪些记录。也就是说,insert_id无法获取任何正在插入的内容。

如果你想获得之前添加的Last id,你可以这样做。

$lastid=$this->db->query("SELECT MAX(member_id) FROM member");

最新更新