Christian
Christian

Reputation: 269

Codeigniter insert array into database

I am currently making a registration form where in you could register individually or by many I need a way how to make the multiple register work i cant add the input into db i get an array to string conversion error i still dont have the model for this

my code is controller

public function registerbatch(){
            for ($i = 0; $i < count($this->input->post('surname','firstname','age','school','course','email')); $i++) {
                $this->form_validation->set_rules("surname[$i]", "surname[$i]", "required");
                $this->form_validation->set_rules("firstname[$i]", "firstname[$i]", "required");
                $this->form_validation->set_rules("age[$i]", "Age[$i]", "required");
                $this->form_validation->set_rules("school[$i]", "School[$i]", "required");
                $this->form_validation->set_rules("course[$i]", "Course[$i]", "required");
                $this->form_validation->set_rules("email[$i]", "Email[$i]", "required");
            }
            if ($this->form_validation->run() == TRUE) {

                $reg_dat = array(

                    'surname' => $this->input->post('surname'),
                    'name' => $this->input->post('firstname'),
                    'age' => $this->input->post('age'),
                    'school' => $this->input->post('school'),
                    'course' => ($this->input->post('course')),
                    'email' => ($this->input->post('email')),

                );
             $this->user_model->add_user($reg_dat);
             $this->load->view('user/home_view');
            } else {
                $this->load->view('user/batch_register');
            }

view:

<html>
<head>
</head> 
<body>  
    <form class="form" action="<?php echo base_url() . 'user/registerbatch'; ?>" method="post" class="form-horizontal" role="form">


        <?php for ($i = 0; $i < $num; $i++): ?>
        <br>
        Surname: <input type="text" name="surname[]">
        <br>
        Name: <input type="text" name="firstname[]">
        <br>
        Age:<input type ="int" name ="age[]">
        <br>
        School: <input type="text" readonly value="<?= $school ?>" name="school[]">
        <br>
        Course:<input type ="text" name ="course[]">
        <br>
        Email:<input type ="text" name ="email[]">
        <br>
        <br>


    <?php endfor ?>
   <button type="submit" class="btn btn-success">Register</button>


</body>
</html> 

Upvotes: 0

Views: 10428

Answers (2)

msvairam
msvairam

Reputation: 872

Try this below coding ....

  if ($this->form_validation->run() == TRUE) {

  extract($_POST);

  foreach($surname as $key=>$value) {

                $reg_dat = array(

                    'surname'   => $value,
                    'name'      => $firstname[$key],
                    'age'       => $age[$key],
                    'school'    => $school[$key],
                    'course'    => $course[$key],
                    'email'     => $email[$key],

                );
             $this->user_model->add_user($reg_dat);

}

}

$this->load->view('user/home_view');

Upvotes: 4

David
David

Reputation: 1145

Seems that your Post can be a multidimensional array. I think the best way to solve your problem is to foreach that post and insert every row

       //your controller
       if ($this->form_validation->run() == TRUE) {
           $reg_dat_multi = $this->input->post(); 
           foreach  ($reg_dat_multi as $reg_dat) {
               $this->user_model->add_user($reg_dat);
            }
       );

you didn't show your model but let's think that is something like this

    //your model
    function add_user($reg_dat){
        if ( $this->db->insert('table', $reg_dat) ){
            return true;
        }
        return false; 
    }

hope that helps

Upvotes: 1

Related Questions