how to get last insert id after insert query in codeigniter active record

0 votes

I have an active record style insert query which has been used to insert the form fields into a MySQL table. I want to get the last auto-incremented id for the insert operation as the return value of my query but have been facing issues with it. Have shared my code below:-

Inside the controller:

function add_post(){ 
        $post_data = array( 
              'id' => '', 
              'user_id' => '11330', 
              'content' => $this->input->post('poster_textarea'), 
              'date_time' => date("Y-m-d H:i:s"), 
              'status' => '1' 
    ); 
    return $this->blog_model->add_post($post_data); 
}


Inside model:

function add_post($post_data){ 
        $this->db->trans_start(); 
        $this->db->insert('posts',$post_data); 
        $this->db->trans_complete(); 
        return $this->db->insert_id(); 
}

I get nothing as the return of the add_post in the model. Could anyone please help me with this? 

Feb 23, 2022 in Others by Rahul
• 9,680 points
4,293 views

1 answer to this question.

0 votes

To answer your doubt, try the following lines of code:-

function add_post($post_data){ 
      $this->db->insert('posts', $post_data); 
      $insert_id = $this->db->insert_id(); 

      return $insert_id; 
}

In case of multiple inserts you could use:-

$this->db->trans_start(); 
$this->db->trans_complete();

answered Feb 23, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
3,611 views
0 votes
0 answers

To query Last 7 days data in DynamoDB

HashKey(Date) ,RangeKey(timestamp) This is what my dynamo DB ...READ MORE

Apr 9, 2022 in Others by Kichu
• 19,040 points
1,583 views
0 votes
0 answers

How to get response from S3 getObject in Node.js?

I am attempting to get data back ...READ MORE

Apr 23, 2022 in Others by Kichu
• 19,040 points
1,883 views
0 votes
1 answer
0 votes
0 answers
0 votes
0 answers

Get last executed query in MySQL with PHP/CodeIgniter

In my_model.php, I have: $query1 = ( ...something... ); $query2 ...READ MORE

Jul 22, 2022 in PHP by Kithuzzz
• 38,000 points
846 views
0 votes
1 answer

Want a command to be executed in Salt only if a directory is empty

You should consider using this inside your ...READ MORE

answered Jun 12, 2018 in DevOps Tools by Damon Salvatore
• 5,980 points
1,858 views
0 votes
1 answer

How to get current time and date in Android

In order to get the current date ...READ MORE

answered Feb 23, 2022 in Others by Aditya
• 7,680 points
2,163 views
0 votes
1 answer

How to get landmarks from current location using google map in iphone?

You can achieve this by saving several ...READ MORE

answered Sep 20, 2022 in Others by Aditya
• 7,680 points
522 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP