Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

Get id of inserted row and use for the next query?

Im currently pushing myself to do little projects for myself that are well out of my depth but learning a heck of a lot quickly. But there are a few things i have no idea about or if its even possible...

What i'm trying to do is sync a table with another (ish).

What it does is to Grab all the Reservations and create a Event from those reservations in an event calendar (Wordpress).

The problem I'm getting is that when I save a reservation it adds it to the events table but it also needs a post to link to it.

So what im looking to do is:

When it syncs -> get all the bookings and go through each one to see if its in the events table or not.

Then the ones that aren't, I want it to insert the data as below:

    $booking = new Event(array(
                'post_id' => 5281,
                'StartDate' => date('Y-m-d', strtotime($booking->check_in)),
                'EndDate' => date('Y-m-d', strtotime($booking->check_in)),
                'StartTime' => date('G:i:00', strtotime($booking->start_hour)),
                'FinishTime' => date('G:i:00', strtotime($booking->end_hour)),
                'event_occurrence' => 0,
            ));
            $booking->save();     

This then creates the new event. As you can see the 'post_id' is hardcoded to grab a pre made post. But what i noticed is that it doesn't work correctly, so to get around this I wanted to make a new post before the booking and grab the id of the new post and insert it in post_id.

Im really not sure if this is possible or not or how to even go about it.

Heres the layout of the db and the code i have.

// Posts
        // =====
        // ID
        // post_author
        // post_date = NOW()
        // post_date_gmt
        // post_content
        // post_title
        // post_status = publish
        // comment_status
        // ping_status
        // post_name = url slug
        // post_type = event
        // guid =?post_type=event&p=4024 (p=id)

        // Reservations
        // ==========
        // id 
        // calendar_id
        // woo_order_id
        // check_in (date)
        // check_out (date)
        // start_hour
        // end_hour
        
    // Events
        // ======
        // event_id = created at time
        // post_id = 4024
        // StartDate = check_in
        // EndDate = check_out
        // StartTime = start_hour
        // FinishTime = end_hour

And the code:

// Create the bookings
        $bookings = Booking::all();

        foreach($bookings as $booking) { 
            $event = new Post(array(
                'post_author' => 8,
                'post_date' => NOW(),
                'post_date_gmt' => NOW(),
                'post_content' => 'Reserved Booking',
                'post_title' => 'Reserved Booking',
                'post_status' => 'publish',
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_name' => 'reserved' . str_random(3),
                'post_type' => 'event'
            ));
            $event->save();

            $booking = new Event(array(
                'post_id' => 5281,
                'StartDate' => date('Y-m-d', strtotime($booking->check_in)),
                'EndDate' => date('Y-m-d', strtotime($booking->check_in)),
                'StartTime' => date('G:i:00', strtotime($booking->start_hour)),
                'FinishTime' => date('G:i:00', strtotime($booking->end_hour)),
                'event_occurrence' => 0,
            ));

            $booking->save();  
         
        }

        $count = $booking->count();  

        return redirect(action('EventsController@index'))
                ->with('status', $count . ' entries have been added to the database!');
    }

Many thanks

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

You can just simply call-

$postId = $event->id;

This will give you last inserted ID of $event.

Now in your booking, you just add-

'post_id' => $postId,

That should work.

Full Code

$event = new Post(array(
                'post_author' => 8,
                'post_date' => NOW(),
                'post_date_gmt' => NOW(),
                'post_content' => 'Reserved Booking',
                'post_title' => 'Reserved Booking',
                'post_status' => 'publish',
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_name' => 'reserved' . str_random(3),
                'post_type' => 'event'
            ));
            $event->save();

$postId = $event->id;

            $booking = new Event(array(
                'post_id' => $postId,
                'StartDate' => date('Y-m-d', strtotime($booking->check_in)),
                'EndDate' => date('Y-m-d', strtotime($booking->check_in)),
                'StartTime' => date('G:i:00', strtotime($booking->start_hour)),
                'FinishTime' => date('G:i:00', strtotime($booking->end_hour)),
                'event_occurrence' => 0,
            ));

            $booking->save();  
2 likes
skoobi's avatar
Level 13

Ah awesome thank you. Ill give this a go and see how i get on.

1 like

Please or to participate in this conversation.