Write some data to a database in Codeigniter

I have a problem. I have a questionnaire site where students participate as respondents to fill out a site question. The problem is that when sending the data contained in the survey results to the database, the data is not written to the database.

this is my table with the name “jawab” as the answer record using the column:
“NIS” as the student identifier
'ID_SOAL' as the question identifier
“JAWABAN” as the student answers

| NIS | ID_SOAL | JAWABAN |
| ... | ....... | ....... |

this table (jawab) is still empty

now in my control code called "soal" as a question controller:

public function index(){
$data['title'] = "Kriteria Rumah Masa Depan yang di Inginkan"; // <-- tiltle
$data['form_action'] = site_url('soal/index'); // 

//GET QUESTION
$data['soals'] = $this->Soal_model->getSoal();

    // SEND ANSWERS TO DATABASE  
$JAWABAN = $this->input->post('JAWABAN');
$this->Soal_model->InputJawaban($JAWABAN);

$this->load->view('soal/index', $data);
}

my model code named soal_model as a question model:

/* GET ALL QUESTION */      
function getSoal(){
$this->db->select('ID_SOAL, SOAL');
$query = $this->db->get('soal');
if($query->num_rows() > 0){
return $query->result_array();
}
}

/* **input students answer to database** */     
function InputJawaban($JAWABAN){
    $data = array(
    'JAWABAN' => $JAWABAN  /* JAWABAN as ANSWER */
    ); $this->db->insert('jawab',$data); 
    }

"index":

    <body>  
<form method="post" action="<?php echo $form_action; ?>">
<table>
    <tr>
        <th>NO</th>
        <th>SOAL</th>
        <th>JAWAB</th>
    </tr>
    <?php $i= 1; ?>
    <?php foreach($soals as $soal): ?>
    <tr>
        <td><?php echo $soal['ID_SOAL']; ?></td>
        <td><?php echo $soal['SOAL']; ?></td>
        <td><input name="JAWABAN<?php echo $i;?>" type="radio" value="1">YA</input>
            <input name="JAWABAN<?php echo $i;?>" type="radio" value="0">TIDAK</input></td>     
            <?php $i++; ?>  
    </tr>
    <?php endforeach;?> 
</table>
<input type="submit" value="KIRIM"/>
</form>

</body>

"YA" "TIDAK"

, IDEA

| NIS | ID_SOAL | JAWABAN |
| 001 | 1 | Ya |
| 001 | 2 | Ya |
| 001 | 3 | Tidak |
| 001 | 4 | Tidak |

, ,

+4
1

, , html .

<?php foreach($soals as $soal): ?>
<tr>
<td><?php echo $soal['ID_SOAL']; ?></td>
<td><?php echo $soal['SOAL']; ?></td>
<td>
<input name="JAWABAN[<?php echo $soal['ID_SOAL']; ?>]" type="radio" value="1" />YA
<input name="JAWABAN[<?php echo $soal['ID_SOAL']; ?>]" type="radio" value="0" />TIDAK
<input type="hidden" name="question[<?php echo $soal['ID_SOAL']; ?>]" value="<?php echo $soal['ID_SOAL']; ?>" />     
</td>     
<?php $i++; ?>  
</tr>
<?php endforeach;?>

, .

$this->Soal_model->InputJawaban($_POST);

, , , , JAWABAN,ID_SOAL NIS 1 ,

function InputJawaban($data)
{
    if (empty($data)) {
        return false;
    }
    foreach ($data['question'] as $key => $qid) {
        $answer = $data['JAWABAN'][$qid]; /* $qid is question id and from that id
        i have picked the answer whose name contains that question id i*/
        $data = array(
            'JAWABAN' => $answer,
            'ID_SOAL' => $qid,
            'NIS'   =>1
        );
        $this->db->insert('jawab', $data);
    }
    return true;

}
+1

Source: https://habr.com/ru/post/1537082/


All Articles