Inserting multiple rows in MySQL in Codeigniter

This post is on inserting multiple rows to a mySQL database table using CodeIgniter is supported by CodeIgniter

CodeIgniter support inserting multiple rows (batch insert) to a database using insert method. 


 $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); 

 If the data type is array of array then CodeIgniter automatically determine this as the batch insert and generate query accordingly. When the data type is array a simple insert query is used.

 
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') 

 

Generate your website

Do you know that the PHP Code Generator can generate PHP websites in minutes in CodeIgniter 3?

Learn more about our PHP Code Generator and generate your web applications using PCG with functioning admin portal.

Connect with Google or Facebook and generate your next CodeIgniter website in minutes with plenty of features like searching with pagination, AJAX based grid data editors, group based security and admin portal etc.