[英]last inserted id in Php codeIgniter
In my model I have return below function for get record id 在我的模型中,我有下面的函数获取记录ID
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion 现在我想将该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);
}
}
Now I want to access model function in controller and pass record id in data function How I do ?? 现在我想访问控制器中的模型函数,并在数据函数中传递记录ID。我该怎么办?
set return in model and in controller write 在模型和控制器写入中设置收益
$insert_id=$this->db->insert_id(); $ insert_id = $ this-> db-> insert_id();
In Controller load your model first using 在Controller中首先使用加载模型
$this->load->model('model_name');
Then call to model's getLastInserted() function. 然后调用模型的getLastInserted()函数。
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid;
在模型中return $query->row()->userid;
insead return of $query->result()
. return of $query->result()
insead return of $query->result()
。
Then modify the controller: 然后修改控制器:
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);
}
}
hi i worked out the solution on last inserted id dear here the code: 您好,我在最后插入的ID上解决了此问题,代码如下:
controller: 控制器:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model: 模型:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model: 模型:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.