2017-01-21 6 views
3

방금 ​​데이터 쿼리 빌더를 사용하여 mysql에 데이터를 추가했습니다. 이제 해당 데이터 (행)의 기본 키 (트랙 코드)를 다시 검색해야합니다. 나는 삽입과 select 쿼리를 동시에 사용했지만 효과가 없었습니다.codeigniter에 방금 삽입 된 데이터의 primary_key를 검색하십시오.

모델 :

public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip) 
     { 
      $data = array(
       'cName' => $cName, 
       'vName' => $vName, 
       'Email' => $Email, 
       'Contact' => $Contact, 
       'Date' => $date, 
       'Complain' => $Complain, 
       'ip' => $ip 
       ); 
      $sql= $this->db->set($data)->get_compiled_insert('tbl_complain'); 
      $q=$this->db->query($sql); 
      return $q; 
     } 

컨트롤러 : 마지막으로 삽입 된 ID가 필요한 경우

public function index() 
    { 
     if (isset($_POST['btnRegister'])) 
     { 
      $cName=$this->input->post('cName'); 
      $vName=$this->input->post('vName'); 
      $Email=$this->input->post('email'); 
      $Contact=$this->input->post('phone'); 
      $date=$this->input->post('Date'); 
      $Complain=$this->input->post('complain'); 
      $ip=file_get_contents("http://ipecho.net/plain"); 
      $this->HamroSamajModel->complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip); 
      $this->session->set_flashdata("message","Your complain has been registered sucessfully"); 
     } 
     $this->load->view('Complain/index'); 
    } 
+0

기본 키 ID를 의미 시도? –

+0

예. 데이터를 삽입 한 후 데이터를 삽입하는 데 사용하는 ID ... 페이지의 기본 키를 추적 코드로 표시하지 않으려 고합니다. – Aryan

+0

아래에서 확인하십시오. –

답변

1

그냥이

public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip) 
{ 
    $data = array(
     'cName' => $cName, 
     'vName' => $vName, 
     'Email' => $Email, 
     'Contact' => $Contact, 
     'Date' => $date, 
     'Complain' => $Complain, 
     'ip' => $ip 
    ); 

    $this->db->insert('tbl_complain', $data); 
    $lastID = $this->db->insert_id(); 
    return $lastID; 
}