2012-09-26 3 views
0

안녕 얘들 아 약간의 도움이 필요합니다. 내가 원한다면 다른 테이블에 전달하기 위해 내 member_id를 검색하는 것입니다. $ this-> db-> insert_id();를 사용하려고했습니다. 하지만 그것은 0으로 돌아갑니다. 그러나 테이블에서 ID 값은 8입니다. 어떻게 다른 테이블에서 8의 값을 얻을 수 있습니까? 왜냐하면 나는 한 번의 실행으로 두 개의 쿼리를 실행하려고하기 때문입니다. 여기 내 샘플 코드가 있습니다.삽입 된 코드 검색 Codeigniter

$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); 

그리고 내 다른 문제는 내가 테이블에 데이터를 삽입 할 때입니다. 그것은 단지 두 번째 테이블에 int를 삽입했습니다. 내가 원하는

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; 

먼저 멤버를 삽입하는 것입니다 : 여기 내 테이블 구조입니다. 멤버가 참조 아이디가없는 경우 멤버에 자동으로 추가되고 upline 필드에 'none'값을 삽입합니다. 참조 ID는 회원의 사용을 언급 한 사람들로부터 온 것입니다. 그리고 구성원이 참조 ID를 가지고 있으면 멤버 테이블과 다운 라인 테이블에도 추가됩니다. 그러나 다운 라인 테이블은 사용자 수준이 맞는지 확인합니다. 나는 수준의 룩업에 문제가 없다. 내 문제가 내 하위 행에 있습니다. 그것은 항상 $ this-> db-> inser_id()에서 오는 값 0으로 저장합니다. 어떻게 올바른 값을 얻을 수 있습니까? 제발 도와주세요.

답변

0

$this->db->insert('member',$member); 호출은 아마도 마지막으로 삽입 된 ID를 읽고 시도한 후 downline 테이블에 사용하기 때문에 호출이 더 가까이 배치되어야한다고 생각합니다! 그렇지 않으면 $this->db->insert_id()에 전화 할 때 실제로 아무것도 삽입되지 않았으므로 읽을 내용이 없습니다.

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

이 쿼리는 데이터베이스에 경우에만 직후 id를 반환

+0

나는 당신의 제안을 시도 할 것이다. 희망이 작동합니다. – rochellecanale

0

코드는

편집해야합니다. $member_insert_id = $this->db->insert_id(); 전에 멤버 테이블에 레코드를 삽입 한 위치를 알 수 없습니다. insert_id가 가져올 수있는 것은 아무것도 삽입되지 않습니다.

이전에 추가 한 신분증을 가져 오려면 다음과 같이하십시오.

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