Php codeIgniter中最后插入的id
在我的模型中,我有下面的返回函数来获取记录idPhp codeIgniter中最后插入的id,php,codeigniter,Php,Codeigniter,在我的模型中,我有下面的返回函数来获取记录id function getLastInserted() { $query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration"); return $query->result(); } 现在我想将该ID传递给mycontroller以插入记录 public function newregistration() { i
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。我是如何做的???在控制器中首先使用
$this->load->model('model_name');
然后调用模型的getLastInserted函数
$ID = $this->model_name->getLastInserted();//you will get id here
在模型中返回$query->row->userid;insead返回$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);
}
}
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
在模型和控制器写入中设置返回
$insert\u id=$this->db->insert\u id 您好,我在上次插入的id上找到了解决方案亲爱的,下面是代码: 控制器:
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);
}
}
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
型号:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
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);
}
}
型号:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
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);
}
}
它在下面显示了错误1-消息:尝试获取非对象文件名的属性:models/adminmodel.php和2-controllerpublic函数newregistration{$this->load->model'adminmodel';if$this->form_validation->run中的“UserID”列不能为null{$data=array'UserID'=>$this->adminmodel->getLastInserted,'Name'=>$this->input->post'Name','MobileNo'=>$this->input->post'MobileNo','IMEINumber'=>$this->input->post'City';}函数getLastInserted{$query=$this->db->selectMAXUserID+1作为userid->getregisterationmaster;return$query->result->userid;}将其更改为答案中的eturn$query->result->userid。您的意思是现在我想将该ID传递给mycontroller以进行记录插入您可以共享insertregistration函数的代码吗?公共函数newregistration{if$this->form\u validation->run{$data=array'UserID'=>$this->adminmodel->getLastInserted,'Name'=>$this->input->post'Name','MobileNo'=>$this->input->post'MobileNo','IMEINumber'=>$this->input->post'City';$this->adminmodel->insertregistration$data;}