0

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> 
3
  • print_r( $reg_dat ); check the result and share add_user() coding Commented Jan 27, 2016 at 12:20
  • this is my model public function add_batchuser($reg_dat) { $this->db->set('date_registered', 'NOW()', FALSE); $this->db->insert('user', $reg_dat); } Commented Jan 28, 2016 at 1:18
  • and the return value when printing reg_dat i tried to run this and print_reg Array ( [surname] => Array ( [0] => surnametest [1] => testsurname ) [name] => Array ( [0] => nametest [1] => testname ) [age] => Array ( [0] => 20 [1] => 30 ) [school] => Array ( [0] => testschool [1] => testschool ) [course] => Array ( [0] => CS [1] => EE ) [email] => Array ( [0] => [email protected] [1] => [email protected] ) ) Commented Jan 28, 2016 at 1:19

2 Answers 2

4

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'); 
Sign up to request clarification or add additional context in comments.

3 Comments

i just noticed this when I am inserting into database it inserts the data twice resulting into a duplticate in entry any ideas?
Share your add_user() function in your question and check get value is twice from view form print_r($_POST);
oh wait nevermind i just had an unclosed bracket in my for loop thanks !
1

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

1 Comment

i tried this but im getting a database error You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0, 1) VALUES (NOW(), 'snamtest', 'testsname')' at line 1 INSERT INTO user ( 0, 1) VALUES ( 'snamtest', 'testsname')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.