0

UPDATED. I'm having problem about inserting multiple checkbox data in the database.

Here is my view

 <?php 
            $i = 0;
            $qArr = array();
            $qArrc = 0;
            $qArr1 = array();
            $qArrc1 = 0;
            $uArr = array();
            $u = 0;
            foreach($questions->result() as $q){ ?>
                <input type="checkbox" name="check[<?php echo $i; ?>]" value="<?php echo $qArr[$qArrc++] = $q -> questions; ?>"> <?php echo $q->questions; ?> <br>
                <input class="form-control" value="<?php echo $uArr[$u++] = $this->session->userdata('user_id'); ?>" name="hidden[<?php echo $i; ?>]" type="hidden">
                <input class="form-control" value="<?php echo $qArr1[$qArrc1] = $q->id; ?>" name="hidden1[<?php echo $i; ?>]" type="hidden">
            <?php 
            $i++;                               
            }?>
            <button type="submit" name="submit" class="btn btn-primary">Submit</button>

Here's my controller

                for($i = 0; $i<count($this->input->post('check')); $i++){
                $data1 = array(  
                    'question' => $this->input->post('check')[$i],
                    'speaker_id' => $this->input->post('hidden')[$i],
                    'question_id' => $this->input->post('hidden1')[$i]
                );  
                $this->input->post('submit');  

                     $this->Speaker_Model->insert_speakerfeedback($data1);  


            }redirect('speaker/createfeedback');

Here's my model

      public function insert_speakerfeedback($data1){
        $this->db->insert("speakerdata", $data1);
    }
3
  • 2
    You do redirect after first insert, what do you expect? Commented Apr 14, 2019 at 13:44
  • After for loop. Commented Apr 14, 2019 at 13:48
  • Ow. Yes you're right. Thanks man Commented Apr 14, 2019 at 13:49

1 Answer 1

1

Modify your for loops :

for($i = 0; $i<count($this->input->post('check')); $i++){

to use foreach instead` :

foreach ($this->input->post('check') as $i => $value) {

So it will prevent getting undefined index if you skip some checkbox. And the redirect line should be outside of the loops.

For bulk insert, you could use insert_batch function.

Within the loop, change :

$data1 = array(

to

$data1[] = array(

So it will not overwriting on each iteration.

And on the Model, change :

$this->db->insert("speakerdata", $data1);

to :

$this->db->insert_batch("speakerdata", $data1);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi I already update the question, the only problem now is how to insert multiple checkbox datas. But thanks for your suggestion!
Well this suggestion is helpful too!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.