Skip to content

Insert multiple rows with CodeIgniter

Last updated on February 1, 2018

In this post, I will show you how to insert multiple rows in CodeIgniter by using CI’s active records library. CI active record has a function insert_batch().By using this function we can insert multiple rows with a single query and You can either pass an array or an object to the function.

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

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

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments