我有insert_id的问题。我正在使用CodeIgniter。我有一个方法,我插入一些值,并在其中返回
$insert_id=$this->db->insert_id();
return $insert_id;`
我想在同一模型中的另一个方法中使用此值。我尝试过:$insert_id=$this->add_teacher_subject();
但是这样第一种方法再次运行,我没有收到last_insert_id,但这个值+1。请告诉我如何解决这个问题?
我的模特是:
public function add_teacher_subject() {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'user_id'=>$this->session->userdata['user_id'],
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$insert_id=$this->db->insert_id();
return $insert_id;
}
public function survey_fill()
{
$insert_id=$this->add_teacher_subject();
if ($this->input->post('answer')) {
$answers= $this->input->post('answer');
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}