How to insert multiple rows in Codeigniter?

July 19, 2018 | Category : MySQL Codeigniter PHP

In this tutorial, we will learn how to insert multiple records into a MySQL table using CI's active records library. Sometime, We may require to insert multiple rows instead of single records. It also takes a few time and adds multiple rows in a database table.

Codeigniter 3 have an active records library and they provide insert_batch() to create multiple records at a time. So I also added a small example, so you can see that and learn how it can be possible.

Insert Multiple Records:

$insertArray = [

[

'name' => 'My Name',

'detail' => 'My Detail'

],

[

'name' => 'My Name',

'detail' => 'My Detail'

]

];

$this->db->insert_batch('mytable_name', $insertArray);

As you can see above syntax example and understand how it works and easy to use.

So, just do that way.

I hope you found your best.