Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Insert Multiple Rows in SQL

I am fresh to Codeigniter. I have a form which looks something like this.

<tr>
<td><input type="text" name="Name[0]" value=""></td>
<td><input type="text" name="Address[0]"  value=""><br></td>
<td><input type="text" name="Age[0]" value=""></td>
<td><input type="text" name="Email[0]" value=""></td>
</tr>



<tr>
<td><input type="text" name="Name[1]" value=""></td>
<td><input type="text" name="Address[1]"  value=""><br></td>
<td><input type="text" name="Age[1]" value=""></td>
<td><input type="text" name="Email[1]" value=""></td>
</tr>

There may be from 0 to n rows, usually 5 to 10 rows. How do I insert them in SQL? Is this possible with Codeigniter or should I use a native PHP script?


$name=$_POST['Name'];
$address=$_POST['Address'];
$age=$_POST['Age'];
$email=$_POST['Email'];
$count = count($_POST['Name']);



for($i=0; $i<$count; $i++) {
$data = array(
           'name' => $name[$i], 
           'address' => $address[$i],
           'age' => $age[$i],
           'email' => $email[$i],

           );


  $this->db->insert('mytable', $data);
}

I did this. It works. But the solution seems inelegant.

kevtrout's answer looks better but is currently throwing a lot of errors.

Is there any way to insert all data at one go?

like image 297
Mr Hyde Avatar asked Oct 03 '10 10:10

Mr Hyde


People also ask

How to insert multiple rows in Mysql using CodeIgniter?

php $Mydata = array( array( 'name' => 'Your Name' , 'email' => 'Your Email' , 'course' => 'Your Course' ), array( 'name' => 'Your Name' , 'email' => 'Your Email' , 'course' => 'Your Course' ) ); $this->db->insert_batch('mytable', $Mydata); //OR $post = $this->input->post(); for ($i = 0; $i < count($post['name']; $i++) ...

What is Insert_batch?

insert is for inserting one record and insert_batch is for inserting multiple records.


2 Answers

Multiple insert/ batch insert is now supported by codeigniter. It will firing one query rather than firing too many queries.

$data =array();
for($i=0; $i<$count; $i++) {
$data[$i] = array(
           'name' => $name[$i], 
           'address' => $address[$i],
           'age' => $age[$i],
           'email' => $email[$i],

           );
}

$this->db->insert_batch('mytable', $data);
like image 127
Somnath Muluk Avatar answered Oct 17 '22 17:10

Somnath Muluk


Make your form like this:

<tr>
    <td><input type="text" name="user[0][name]" value=""></td>
    <td><input type="text" name="user[0][address]" value=""><br></td>
    <td><input type="text" name="user[0][age]" value=""></td>
    <td><input type="text" name="user[0][email]" value=""></td>
</tr>
<tr>
    <td><input type="text" name="user[1][name]" value=""></td>
    <td><input type="text" name="user[1][address]" value=""><br></td>
    <td><input type="text" name="user[1][age]" value=""></td>
    <td><input type="text" name="user[1][email]" value=""></td>
</tr>

Then you can simply do:

foreach($_POST['user'] as $user)
{
    $this->db->insert('mytable', $user);
}
like image 8
Mischa Avatar answered Oct 17 '22 17:10

Mischa