2016-11-07 39 views
0

我想將我的測驗應用程序的答案插入到數據庫中。我正在使用Codeigniter。測驗問題是隨機產生的。向用戶顯示10個問題。以下是我的代碼:在數據庫中插入變量名稱的單選按鈕的值

視圖

<div class="col-lg-8"> 
     <table class="table" style="width: 100%;"> 
      <?php 
      $i = 1;    
      echo form_open('Menu/submit_ans', array('name' => 'quiz')); 
      foreach ($quiz_array as $q){ 
      ?>  
      <td colspan="2"style="background-color: #337ab7;color:white;"> <h4>Question No. <?php echo $i?> </h4></td> 
      <tr> 
      <td colspan="2"><?php echo $q->ques;?></td> 
      <input hidden name="qid[]" type="text" value="<?php echo $q->qid;?>"> 
      <input hidden name="uid[]" type="text" value="<?php echo $user['id'];?>"> 
      </tr> 
      <tr> 
      <td><?php echo $q->ans_1;?></td> 
      <td><input type="radio" name="ans[<?php print $i; ?>]" value="1"></td> 
      </tr> 
      <tr> 
      <td><?php echo $q->ans_2;?></td> 
      <td><input type="radio" name="ans[<?php print $i; ?>]" value="2"></td> 
      </tr> 
      <tr> 
      <td><?php echo $q->ans_3;?></td> 
      <td><input type="radio" name="ans[<?php print $i; ?>]" value="3"></td> 
      </tr> 
      <tr> 
      <td><?php echo $q->ans_4;?></td> 
      <td><input type="radio" name="ans[<?php print $i; ?>]" value="4"></td> 
      </tr> 


      <?php 
       $i++; 
      } 
      ?> 
     </table> 
     <center><button class="btn btn-success" type="submit">Submit!</button></a></center> 
    </div> 
      <?php echo form_close();?> 

控制器:

function submit_ans(){ 
    $data = array(
    $q_array = $_POST['qid'], 
    $u_array = $_POST['uid'], 
    $ans_array = $_POST['ans'] 
    ); 
    $this->load->model('MyModel'); 
    $this->MyModel->insert_ans($data); 
} 

模型

功能insert_ans($數據){

foreach($data as $answer) { 
    var_dump($answer); 

    } 

} 

的vardump結果

//爲問題ID

array (size=12) 
    0 => string '4' (length=1) 
    1 => string '12' (length=2) 
    2 => string '3' (length=1) 
    3 => string '6' (length=1) 
    4 => string '11' (length=2) 
    5 => string '7' (length=1) 
    6 => string '10' (length=2) 
    7 => string '2' (length=1) 
    8 => string '8' (length=1) 
    9 => string '1' (length=1) 
    10 => string '5' (length=1) 
    11 => string '9' (length=1) 

//用戶ID

array (size=12) 
    0 => string '1' (length=1) 
    1 => string '1' (length=1) 
    2 => string '1' (length=1) 
    3 => string '1' (length=1) 
    4 => string '1' (length=1) 
    5 => string '1' (length=1) 
    6 => string '1' (length=1) 
    7 => string '1' (length=1) 
    8 => string '1' (length=1) 
    9 => string '1' (length=1) 
    10 => string '1' (length=1) 
    11 => string '1' (length=1) 

答案

array (size=12) 
    0 => string '1' (length=1) 
    1 => string '1' (length=1) 
    2 => string '3' (length=1) 
    3 => string '4' (length=1) 
    4 => string '2' (length=1) 
    5 => string '2' (length=1) 
    6 => string '3' (length=1) 
    7 => string '3' (length=1) 
    8 => string '4' (length=1) 
    9 => string '3' (length=1) 
    10 => string '2' (length=1) 
    11 => string '3' (length=1) 

我得到的一切,我被從提問想要的結果// ID來回答。我現在如何在數據庫中插入這個表?

以下是我的表,由4列:

ans_id - 自動增量 USER_ID - 用戶誰回答問題 q_id - 回答過的問題 ans_att - 答案

回答

0

嘗試設置無線名稱作爲數組:

<input type="radio" name="asw[]" value="1"></td> 
<input type="radio" name="asw[]" value="1"></td> 

,併爲包括在你的數據庫,試試這個PHP:

$asw = $_POST['asw']; 

foreach($asw as $answer){ 
mysql_query("insert into table_name (col_name) values ('". $aswer ."')"); 
} 

這是你想要的嗎?

0

你不能有一個隨機名稱,因爲這最後會插入到你的數據,所以你可以嘗試修復名稱,隨機值和ID。嘗試這個與Java腳本功能 「type =」radio「name =」as「value =」「> 但請你給我們你的表更清晰

+0

請檢查我的編輯 –

+0

這是一條評論,不是答案 – RST

相關問題