php - Codeigniter insert array into database -
i making registration form in register individually or many need way how make multiple register work cant add input db array string conversion error still dont have model this
my code controller
public function registerbatch(){ ($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 ($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>
try below coding ....
if ($this->form_validation->run() == true) { extract($_post); foreach($surname $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');
Comments
Post a Comment