我在CodeIgniter中的模型中有一个函数,我在数据库中插入,然后返回$result=$this->db->insert_id();
return $result;
。我在这个模型中有另一个函数,我在另一个表中插入其他值,我需要插入$result=$this->db->insert_id();
return $result;
的列。但是正如我所做的那样,function add_teacher_subject()
因为这个而再次运行:$insert_id=$this->add_teacher_subject();
并且当第二个函数执行时我例如没有insert_id = 5,但是insert_id = 6。如何从第一个函数插入第二个函数insert_id?
这是我的代码:
public function add_teacher_subject() {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'subject_id'=>$this->input->post('subject'),
'survey_id'=>$this->uri->segment(3),
'user_id'=>$this->session->userdata['user_id'],
'survey_code'=>$this->input->post('random_code'),
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$result=$this->db->insert_id();
return $result;
}
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,
'created_at'=>$date->format('Y-m-d H:i:s'),
'student_survey_id' => $insert_id //I want $insert_id from function survey_fill() How to get it
);
$this->db->insert('survey_answers', $data);
}
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
问题:我试图将这两个函数合并为一个,从第一个使用insert_id,然后我使用了参数,但它仍然没有我想要的结果。当我第一次调用add_teacher_subject()来插入$this->db->insert('student_surveys',$data);
时,我想要如何使用这些参数。当我第二次调用它时,要使用第一个查询中的insert_id:$insert_id=$this->db->insert_id();
我的模型现在是:
public function add_teacher_subject($subject=0, $survey) {
if($subject) {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'subject_id'=>$this->input->post('subject'),
'survey_id'=>$this->uri->segment(3),
'user_id'=>$this->session->userdata['user_id'],
'survey_code'=>$this->input->post('random_code'),
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$insert_id=$this->db->insert_id();
}
if($survey) {
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,
'created_at'=>$date->format('Y-m-d H:i:s'),
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
return $insert_id;
if($this->db->affected_rows() > 0)
{
return true;
}
} return false;
}
}
我的控制器是:
public function survey_fill()
{
//form_validation for teacher and subject
if ($this->form_validation->run()==FALSE)
{
$this->survey_show();
}
else
{
$survey=$this->uri->segment(3);
if ($this->user_model->add_teacher_subject($survey))
{
echo "You filled survey!";
echo "<script>setTimeout(\"location.href = '/survey/index.php/index/surveys_show';\",1500);</script>";
}
}
}
public function student_surveys() {
//form validation for survey answers
if ($this->form_validation->run()==FALSE)
{
$this->student_surveys_show();
}
else
{
$subject=$this->input->post('subject');
$this->user_model->add_teacher_subject($subject);
if($this->db->affected_rows() > 0)
{
$survey_id = $this->uri->segment(3);
redirect('/index/survey_show/' . $survey_id);
}
}