I have foreach loop within a foreach loop, but when I insert into the db, it inserts more than 1 entry for the each item.. Is my foreach loop wrong for what I am trying to do.
VIEW
CONTROLLERHTML Code:<input type="hidden" name="question_id[]" value="<?php echo $j->id;?>"/> <input name="answers[]" id="questions" class="deal_form"/>
MODELPHP Code:$answers = $this->input->post('answers');
$question_id = $this->input->post('question_id');
$this->load->model('model_survey');
foreach($answers as $d) {
foreach($question_id as $q){
$this->model_survey->insert_survey_answers($d,$q);
}
}
PHP Code:function insert_survey_answers($answer,$id){
$query = "INSERT INTO survey_answers (answer,question_id) VALUES(".$this->db->escape($answer).",'".$id."')";
$this->db->query($query);
}



Reply With Quote
Bookmarks