Dies steht mit einer Quizanwendung in Verbindung. Fragen werden zufällig aus der Datenbank generiert. Benutzer wählt eine Antwort über Radiobutton aus. Die Antwort wird zusammen mit der Frage-ID und der Benutzer-ID in der Datenbank gespeichert. Alles funktioniert gut.Array innerhalb eines Arrays in Datenbank in Codeigniter einfügen
Das Problem ist, ich habe ein Array, das ich in meiner Datenbank speichern möchte. Das Folgende ist der Code.
The View:
<div class="col-lg-8">
<table class="table" style="width: 100%;">
<?php $i = 0;
$ques = 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 $ques?> </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></a>
</center>
</div>
<?php echo form_close();?>
Controller:
function submit_ans() {
$data = array(
'qid' => $_POST['qid'],
'user_id' => $_POST['uid'],
'ans_att' => $_POST['ans']
);
$this->load->model('MyModel');
$this->MyModel->insert_ans($data);
}
Modell:
function insert_ans($data) {
foreach($data as $answer) {
//var_dump($answer);
}
$this->db->insert_batch('tbl_answers', $data);
}
Es folgt der Fehler erhalte ich:
INSERT INTO `tbl_answers`() VALUES ('6','9','1','5','7','10','4','12','8','3','11','2'), ('1','1','1','1','1','1','1','1','1','1','1','1'), Array
Wie kann ich das Array in meiner Datenbank speichern?
Verwenden Sie so 'ans ['ans_att'] [ Php print $ i; ?>] 'selbe für alle – devpro