2016-12-11 15 views
0

私はこのプロジェクト用に2つのテーブルを持っています。質問表と回答表。Codeigniterを使用した複数の選択肢のクイズ

Questions Table: と私は別のフィールド名で回答テーブルに挿入された挿入されたすべてのオプションで同じことを質問表にquestion_idのフェイルド値をしたいが

しかし、私はこれらの結果を得た、ことを私は毎回question_id

Answers Table

uはquestion_idsを見ることができるように

は同じではありませんし、私が望むものではないthatsの。ここ

は私のコードである: パブリック関数insert_correct_ans($ ANSWER1、$ ANSWER2、$ ANSWER3、$ answer4) {

$ ID =の$ this - > DB-> INSERT_ID();また、問題id.Soを通過した後、INSERTクエリを書いて、次のcode.Ratherデータをコピーする機能insert_correct_ans()に呼び出した時

$this->db->query("INSERT INTO answers(
              question_id, 
              answer, 
              correct) 
            VALUES(
            '$lastID', 
            '$answer1', '1')"); 

    $this->db->query("INSERT INTO answers(
              question_id, 
              answer, 
              correct) 
            VALUES(
            '$lastID', 
            '$answer2', '0')"); 

    $this->db->query("INSERT INTO 
            answers(
              question_id, 
              answer, 
              correct) 
            VALUES(
             '$lastID', 
             '$answer3', '0')"); 

    $this->db->query("INSERT INTO 
            answers(
              question_id, 
              answer, 
              correct) 
            VALUES(
            '$lastID', 
            '$answer4', '0')"); 
} 
+0

値は '$ lastID'です。そして、あなたがその価値をどのように得ているか。 –

+0

また、この関数で質問IDを送信する必要があります。 –

+0

私は$ this-> db-> insert_id() – courage

答えて

0

繰り返し配列およびユーザーCIのデータベースの挿入機能を作ります。

   <?php 
     public function insert_correct_ans($que_id,$answer1, $answer2, $answer3, $answer4) 
      { 
      $data=array(
       array(
       'question_id'=>$que_id, 
       'answer'=>$answer1, 
       'correct'=>'1' 
       ), 
       array(
       'question_id'=>$que_id, 
       'answer'=>$answer2, 
       'correct'=>'0' 
       ), 
       array(
       'question_id'=>$que_id, 
       'answer'=>$answer3, 
       'correct'=>'0' 
       ), 
      array(
       'question_id'=>$que_id, 
       'answer'=>$answer4, 
       'correct'=>'0' 
       ) 
      ); 
      } 
      $this->db->insert('answers',$data); 
      ?> 
+0

私は自分で質問IDを入力しなければならないという意味ですか? – courage

+0

はい回答を設定するときは、question_idを設定する必要があります。 –

+0

よろしくお願いします... – courage

関連する問題