Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:27:53+00:00 2026-05-24T10:27:53+00:00

I need to do multiple insert/update so i came up with transaction to roll

  • 0

I need to do multiple insert/update so i came up with transaction to roll back if anything goes wrong. Besides, my application should update the record if already exists, and insert if not.

So first of all i try to update a record using a unique id, if it return affected_rows=0 i continue wih insert.

Probably i miss something within transaction/affected rows, it always return affected_rows=0.

Below is the code:

$this->db->trans_start();

   $this->db->where('order_id', $order_id);
   $this->db->where('order_status', 'Active');
   $this->db->update('orders', $order);

   $this->db->where('order_id', $order_id);
   $this->db->where('sku', $item_sku);
   $this->db->update('order_items', $order_items);

$this->db->trans_complete();

if ($this->db->affected_rows()==0){
   $this->db->trans_start();
       $this->db->insert('orders', $order);
       $this->db->insert('order_items', $order_items);
   $this->db->trans_complete();
}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:27:55+00:00Added an answer on May 24, 2026 at 10:27 am

    I use MySQL’s ON DUPLICATE KEY UPDATE to handle that case. However, CodeIgniter’s ActiveRecord implementation does not support this natively, and the accepted answer in this thread:

    How would I use ON DUPLICATE KEY UPDATE in my CodeIgniter model?

    Seems to be dead. So, consider just using raw MySQL instead of the ActiveRecord pattern; this is fairly common amongst CI developers (I do not use their implementation of AR).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I need to update or insert into multiple tables with one action, example
Generally when I need to insert/update multiple rows, I will generate (in PHP) a
I need some help to insert comma separates strings into multiple columns into db.
I am developing LOB application, where I will need multiple dialog windows (and displaying
I have an Access application in which I need to do a 'mass-update'. I
I'm hoping to be able to insert/update multiple rows of a table in a
I have a program in Perl I'm working on where I would need multiple
I need to generate multiple random values under SQL Server 2005 and somehow this
I need to have multiple forms in the same webpage, all of them POSTing
I need to add multiple empty divs to a container element using jQuery. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.