fbpx

Bulk insert update and delete data in codeigniter

Bulk insert update and delete data in codeigniter

Why you need to add bulk data in CodeIgniter….? there are many reasons the one reason is you are fetching the id of a table and inserting that id in a new table.

If you calling insert query in the loop yes you can do that but it can decrease your system performance so how can you insert the values..? Codeigniter proving the bulk inserting functionality using query builder you just need to call the method in your model $this->db->insert_batch() just imagine if you have millions of records inside your loop that means you are calling millions of time the insert query, this is not a good practice if you are working on MVC Framework.

foreach ($afterTripQuery as $sendAfter) {
   $this->db->insert('tableName',array('columnName',$sendAfter['columnName']))
}

Here is the simple solution to your problem, as you can see below you are inserting many records using a single query so its totally depend on you how you are coding in your project.

$arrayBatch =  array();
foreach ($afterTripQuery as $sendAfter) {
    $arrayBatch[] = array('columnName'=>$sendAfter['columnName']);
 }
$this->db->insert_batch('tableName', $arrayBatch);

There are many ways where you can use the Batch insert and update in CodeIgniter for supposing you are creating the blog system where you have a relational database like blogs table and the tags table, in the tags table you are sending the blog’s id as a foreign key in the tags table.

Definitely, you can send the data of the tags in the array from HTML, now break the operation to inserting your blog  step 1:  insert the blog content in the blogs table and get the currently inserted id of the table and send that id to the tags table to insert the tag’s elements below the example code.

Here is model’s method we are inserting the blog content and fetching the currently inserted id.

public function addblog($value)
{
    $this->db->insert('blogs',$value);
   return $this->db->insert_id();
}

Now we can fetch the tag’s data which is coming from the HTML

$data['b_slug'] = $this->input->post('blog_slug',true);//fetching the data from the
$blogtags = array();
foreach ($tags as $tag)
{
   $blogtags[] = array(
      'blog_id'=>$blogId,//this is current blog id
      'tag_name'=>$tag,
      'tag_created'=> date("Y-m-d h:i:sa")
   );
}
$this->db->insert_batch('tags', $arrayBatch);

If you want to update the bulk records in CodeIgniter its very simple just use the update_batch from query builder  $this->db->update_batch(‘mytable’, $data, ‘columnName’);

$blogtags = array();
foreach ($tags as $tag)
{
   $blogtags[] = array(
      'blog_id'=>$blogId,//this is current blog id
      'tag_name'=>$tag,
   );
}
$this->db->update_batch('tags', $arrayBatch,'blog_id');//pass the columnName in the third parameter

So now ho can you delete your multiple records with a single query Its very simple just use the where_in method in query builder  $this->db->where_in(‘columName’,array()).

$blogtags = array();
foreach ($tags as $tag)
{
   $blogtags[] = $tag['tagId'];
   );
}
public function deleteTags($blogtags)
{
 $this->db->where_in('tagId', $data);
 $this->db->delete('tags');
}
shakzee
administrator
My Name is Shehzad Ahmed and I am a full-time developer with over 7+ years of experience. In that time, I’ve worked as a developer with many big and small firms and was teaching web design and development skills to many keen learners and by the time passes I’ve found a great passion and potential on myself as a teacher on this particular field and now I am a full-time teacher and working as a freelancer. Web design and development constantly developing every day with new trends, ideas, and innovations. That’s why nowadays you need to learn and keep yourself up-to-date with everything daily and that’s what I do and that is something that I love because I am so passionate about it! When I have spare time I use to give time to my web development community. Teaching kids, managing online programming groups, attending a conference and making blogs, I like keeping myself up-to-date and engaged in my field always. I have my expertise in (PHP, Codeigniter, Laravel, Bootstrap, WordPress, SQL, Query Optimization, Data Science and Design Patterns). Anyone who wants to learn and know anything about web development and design I am always available for that and would love to help out. I always love to be looking out for new things and that is why I spend a lot of my time learning new techniques and helping other people learn web development and design through various groups and web development tutorials as well as online courses.

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp chat