检索插入的id Codeigniter

时间:2012-09-26 10:19:57

标签: php mysql 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-&gt; db-&gt; inser_id()的0值。如何获得正确的值?请帮帮我们。

2 个答案:

答案 0 :(得分:0)

我认为$this->db->insert('member',$member);调用可能应该进一步放置,因为您尝试读取最后插入的ID并在downline表中使用它!否则,当你拨打$this->db->insert_id()时,实际上还没有插入任何东西,所以没有什么可读的!

答案 1 :(得分:0)

您的代码应为

编辑:

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

只有在对数据库进行查询后才会返回id。我没看到你在$member_insert_id = $this->db->insert_id();之前在成员表中插入记录的位置。也就是说,insert_id无法获取任何内容。

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

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