codeigniter:发布到数据库

时间:2016-01-09 22:55:29

标签: codeigniter post codeigniter-2

我需要帮助才能使用答案向数据库发布问题。但是,我在测试方面遇到了问题。我很感激有人可以检查一切是否正确。我不确定表单(视图)中的帖子操作是否正确。发布显示404 Page Not Found的结果。我不是没有它的URL或服务器。

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助

if($_POST){
        $question = $this->input->post('question_add');

        $answer1 = $this->input->post('answer_01');
        $answer2 = $this->input->post('answer_02');
        $answer3 = $this->input->post('answer_03');

        $answercorrect = $this->input->post('answer_correct');

         $this->quiz->addQuestion($question, $answer1, $answer2,  $answer3,     $answercorrect);
        redirect(base_url().'add');//redirect to your form
    }else{
    $this->load->view('form');//your view here 
    }

答案 1 :(得分:0)

希望这会奏效。

最后从模型addQuestion()返回1。

function addQuestion($question,$answer1, $answer2,  $answer3, $answercorrect) {

    //add questions
    $data = array (
        'quizid' => 5,
        'questions' => $question,
        'answerid' => '4'
    );

    $this->db->insert('questions', $data);

    //get id of questions

    $this->db->select('id');    
    $this->db->where('question', $question);    
    $idQuestion = $this->db->get('questions');  

    //add answer

    $data = array(
        'questionid' => $idQuestion->row()->id,
        'answer' => $answer1
    );

    $this->db->insert('answers',$data); 

  //add answer
    $data = array(
        'questionid' => $idQuestion->row()->id,
        'answer' => $answer2
    );

    $this->db->insert('answers',$data); 

    $data = array(
        'questionid' => $idQuestion->row()->id,
        'answer' => $answer3

    );

    $this->db->insert('answers',$data); 

    $data = array(
        'questionid' => $idQuestion->row()->id,
        'answer' => $answercorrect

    );

    $this->db->insert('answers',$data); 


    //get id of questions

    $this->db->select('id');    
    $this->db->where('answer', $answercorrect); 
    $idQuestion = $this->db->get('answers');    

    //update the question with the correct answer id

    $data = array(
        'answerid' => $question->row()->id

    );

    $this->db->where('question', $question);
    $this->db->update('questions', $data); 

    return 1;

 }

在控制器中

function add() {

    $question = $this->input->post('question_add');

    $answer1 = $this->input->post('answer_01');
    $answer2 = $this->input->post('answer_02');
    $answer3 = $this->input->post('answer_03');

    $answercorrect = $this->input->post('answer_correct');

    $query =$this->quiz->addQuestion($question, $answer1, $answer2,  $answer3, $answercorrect);

if($query==1)
{
 $this->load->view('form_question');  //where you want to redirect 
}

}