2017-03-06 80 views
0

在我的模型我有以下函數返回的獲取記錄ID在Php最後插入的ID笨

function getLastInserted() 
{ 
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration"); 
return $query->result();    
} 

現在我想通過該ID來myController的備案插入現在

public function newregistration() 
{ 
if ($this->form_validation->run()) 
{ 
$data = array(
'Name' => $this->input->post('Name'), 
'MobileNo' => $this->input->post('MobileNo'), 
'IMEINumber' => $this->input->post('IMEINumber'), 
'City' => $this->input->post('City') 
); 
$this->adminmodel->insertregistration($data); 
} 
} 

我想在控制器中訪問模型函數並在數據函數中傳遞記錄ID我該怎麼做?

+0

你是什麼意思***現在我想通過該ID到mycontroller記錄插入*** –

+0

你可以共享代碼插入註冊功能? – Naincy

+0

公共函數newregistration() \t { \t \t \t 如果\t($這 - > form_validation->運行()) \t \t { \t \t \t $數據=陣列( \t \t \t '用戶ID'=> $這 - > adminmodel-> getLastInserted(), \t \t \t '名稱'=> $這個 - >輸入 - >交的( '姓名'), \t \t \t 'MobileNo' => $這個 - >輸入 - >後( 'MobileNo'), \t \t \t 'IMEINumber'=> $這個 - >輸入 - >後( 'IMEINumber'), \t \t \t '市'=> $此 - > input-> post('City') \t \t \t); \t \t \t $ this-> adminmodel-> insertregistration($ data); \t \t} \t \t \t \t \t} – Anil

回答

0

在Controller負載模型首先使用

$this->load->model('model_name'); 

然後調用模型的getLastInserted()函數。

$ID = $this->model_name->getLastInserted();//you will get id here 

在模型return $query->row()->userid; INSEAD return of $query->result()

然後修改,所述控制器:在模型和控制器的寫入

public function newregistration() 
{ 
    if ($this->form_validation->run()) 
    { 
     $data = array(
     'UserID'=> $this->model_name->getLastInserted(), 
     'Name' => $this->input->post('Name'), 
     'MobileNo' => $this->input->post('MobileNo'), 
     'IMEINumber' => $this->input->post('IMEINumber'), 
     'City' => $this->input->post('City') 
     ); 
     $this->adminmodel->insertregistration($data); 
    } 
} 
+0

它顯示了以下錯誤我1 - 消息:試圖讓非對象的屬性 文件名:型號/ adminmodel.php和2 - 列 '用戶名' 不能爲空在控制器 – Anil

+0

嘗試編輯答案。 –

+0

public function newregistration() \t { \t \t $ this-> load-> model('adminmodel'); \t \t如果($這 - > form_validation->運行()) \t \t { \t \t \t $數據=陣列( \t \t \t '用戶ID'=> $這 - > adminmodel-> getLastInserted(), \t \t \t '名稱'=> $這個 - >輸入 - >交的( '姓名'), \t \t \t 'MobileNo'=> $這個 - >輸入 - >交的( 'MobileNo'), \t \t \t「 IMEINumber'=> $ this-> input-> post('IMEinumber'), \t \t \t'City'=> $ this-> input-> post('City') \t \t \t); \t \t \t \t } \t \t } \t功能getLastInserted() \t { \t \t $查詢= $這個 - >分貝 \t \t \t \t \t - >選擇(「MAX('UserID')+1作爲用戶名「) \t \t \t \t \t - > get(」registerationmaster「); \t \t return $ query-> result() - > userid; \t \t } – Anil

1

組返回

$ INSERT_ID = $這個 - > DB-> INSERT_ID();