如何传递帖子中的数据,以便我们可以调用ID?

时间:2019-06-17 03:56:26

标签: php codeigniter

如何传递帖子中的数据,以便我们可以调用id?

因为我们试图获取问题的“ qtopic_id”,但该问题不起作用,并且一直在给我一个空值。

我尝试声明qtopic_id = 19来查看其是否保存在qtopic_id列中。 我不必在下面的列中保存特定的id值,这样它就不会仅保存在该特定的id上,而是会保存在其对应的qtopic_id上。

控制器

public function addChoices(){
$this->form_validation->set_rules('ques','Question','required');
$this->form_validation->set_rules('ch_des1','Choices','required');
$this->form_validation->set_rules('ch_des2','Choices','required');
$this->form_validation->set_rules('ch_des3','Choices','required');
$this->form_validation->set_rules('ch_des4','Choices','required');
$this->form_validation->set_rules('ans','Answer','required');
$this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');




if($this->form_validation->run() ){
echo $qtopic_id ;
$data['ques']    = ($this->input->post('ques'));
$data['ch_des1'] = ($this->input->post('ch_des1'));
$data['ch_des2'] = ($this->input->post('ch_des2'));
$data['ch_des3'] = ($this->input->post('ch_des3'));
$data['ch_des4'] = ($this->input->post('ch_des4'));
$data['ans']     = ($this->input->post('ans'));

if($id=$this->queries->registerChoices($data) )
{

$this->session->set_flashdata('message','Test Added Succesfully');
return redirect('admin/testtwo');
}
else {
$this->session->set_flashdata('message','Failed to Add Test');  
}
return redirect('admin/testtwo');
}   

else {
$this->addQuestion();
}   

}


}

型号:

----已更新---

  public function registerChoices($data) {
              echo $this->input->post('qtopic_id');


            $question_arr = [
                'ques' => $data['ques'],
                'qtopic_id' => 19


            ];

            $choices_arr = [
                'ques'    => $data['ques'],
                'ch_des1' => $data['ch_des1'],
                'ch_des2' => $data['ch_des2'],
                'ch_des3' => $data['ch_des3'],
                'ch_des4' => $data['ch_des4'],
                'ans'     => $data['ans']

            ];

   //          echo "<pre>";
            // var_dump($question_arr);
            // var_dump($choices_arr);
            // exit;

            $this->db->insert('tbl_choices',$choices_arr);
            $this->db->insert('tbl_question',$question_arr);
                    return $this->db->insert_id();


        }

error messages that i encountered

1 个答案:

答案 0 :(得分:0)

您选择寄存器的代码还不清楚。 insert_id既不接受数据,也不进行插入,它只是在执行插入查询后返回最后插入的ID。我想您想要的是这样的:

function registerChoices($data) {

    if ($this->db->insert('tablename', $data)) {
        return $this->db->insert_id();
    }
    return false;

}

用法:

$last_insert_id = $this->somemodel->registerChoices($data);

if ($last_insert_id) {
    echo "item with id $last_insert_id was created!";
} else {
    show_error('Query failed!');
}