2016-01-09 165 views
0

我需要幫助發佈一個問題到數據庫的答案。但是,我遇到了測試問題。我希望有人能檢查一切正確。我不確定表單(View)中的發佈操作是否正確。發佈顯示404頁面的結果未找到。我不是,如果它的URL或服務器。codeigniter:發佈到數據庫

+1

你是不是叫你的add()方法的任何意見。如果你只是測試,你可以從方法內部回顯 - 但否則你需要調用一個視圖,可選地傳遞$ data到它$ this-> load-> view('form_question',$ data); – cartalot

+0

你能幫助編寫命令來解決我的問題嗎? –

+0

你知道HTML支持輸入數組嗎?也許你可以嘗試使用它們,它會讓你的代碼看起來更好,如何實現這一點的例子是使用HTML的name屬性如下它會現在成爲你的文章中的一個數組,玩一下,我相信你可以找到一個很好的乾淨的方式來實現這一點。 – killstreet

回答

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 
    } 
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 
} 

}