I have a custom table that I am updating via wp_ajax action. I am inserting a row and inserting all of the values other than id which is an AUTO_INCREMENT. I have an ajax response that creates a new row on the page with the new data. In my response I need to see if the row was added, and then get the record to display. I can of course print everything other than the id with the variables I have pre defined, but I want to get the dynamically created digit as well! Help! I need an if statement to see when the row has finished saving.

The trouble is here in my php, specifically getting $carrier_id:

add_action('wp_ajax_change_carrier', 'carrier_change_callback');
function carrier_change_callback() {
    global $wpdb;

    //New Carrier
    if ($_POST['newCarrier']){
                'name' => stripslashes($_POST['newName']), 
                'label' => $_POST['newLabel'],
                'service_center_path' => $_POST['newHtm'],
                'image_name' => $_POST['newImage']
        //set the variables to return the new Table Row
        $carrier_name = stripslashes($_POST['newName']);
        $carrier_image = $_POST['newImage'];
        $carrier_label = $_POST['newLabel'];
        $carrier_htm = $_POST['newHtm'];
        $carrier_id = $wpdb->get_row("SELECT id FROM $wpdb->prefix.'bf_carriers' WHERE image_name = $carrier_image");
        <!-- Lots of junk going on in here to display above variables and return the response to my ajax call -->

1 Answer 1


After an insert, the ID generated for the AUTO_INCREMENT column can be accessed with:


Last sentence is just copyed and pasted form the codex.

  • Absolutely perfect! For some reason I was looking at $wpdb->get_row. I change that line to read $carrier_id = $wpdb->insert_id; and all is well in my life. Thank you very much! Commented Jul 26, 2013 at 2:34
  • 1
    You are welcome. Today I wrote 2 hundred lines of code here in wpse and the only OP appreciated my work is the one for I just copyed and pasted 3 lines of code. Life is strange :)
    – gmazzap
    Commented Jul 26, 2013 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.