Newly Inserted Record was not Queried, Immediately after Insertion I have a form element below:

//Calls a function that contains the html for templates table.
$form['myid_templates_editor_table_wrapper'] = array(
    '#type' =&gt; 'item',
    '#markup' =&gt; myid_create_templates_table(),   
);


The function below, populates the element above with the HTML  of a table.

function myid_create_templates_table(){    

    //Retrieve list of templates in the database
    $query = db_select('studidtemplates', 't1',array('target' =&gt; 'import'));

    $result = $query
        -&gt;fields('t1', array('template_id', 'template_name', 'date_created', 'user_name'))
        -&gt;orderBY('template_id', 'DESC')
        -&gt;execute();

    $table_header = array(
        'template_id' =&gt; array('data' =&gt; 'Template ID' , 'field' =&gt; 'template_id','sort' =&gt; 'asc'), 
        'template_name' =&gt; array('data' =&gt; t('Template Name'), 'field' =&gt; 'template_name','sort' =&gt; 'asc'),  
        'date_created' =&gt; array('data' =&gt; 'Date Created' , 'field' =&gt; 'date_created','sort' =&gt; 'asc'), 
        'user_name' =&gt; array('data' =&gt; t('Created by'), 'field' =&gt; 'user_name','sort' =&gt; 'asc'),  
    );

    foreach ($result as $record) {          
        $table_rows[] = array(l(t($record-&gt;template_id), $record-&gt;template_id, array('attributes' =&gt; array('class' =&gt; 'use-ajax'))), $record-&gt;template_name, $record-&gt;date_created, $record-&gt;user_name);
    }       
    return '&lt;div id="myid_templates_editor_table_wrapper"&gt;' . theme('table', array('header' =&gt; $table_header, 'rows' =&gt; $table_rows)) . '&lt;/div&gt;';
}


It looks like the image below:



Whenever a new record is inserted to the table, the function below renders the $form['myid_templates_editor_table_wrapper'] element to update it's table display.

function myid_save_templates_editor($form, &amp;$form_state){
    global $user; 

    $save = db_insert('studidtemplates',array('target' =&gt; 'import')) 
        -&gt;fields(array(         
            'template_name' =&gt; 'WEW',            
            'date_created' =&gt; date("Y-m-d H:i:s", time()),
            'user_name' =&gt; $user-&gt;name
        ))       
        -&gt;execute();

    return array(
        '#type' =&gt; 'ajax',
        '#commands' =&gt; array(            
            ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),  

        )
    ); 
}


I notice something weird about it. When my actual record in the database is 1, the table displays 0 records. When my actual record is 2, my table displays 1 record. When my actual record is 3, it displays 2 records in my table and so on and so forth.

I was suspecting that my $form['myid_templates_editor_table_wrapper'] element was being rendered, even if db_insert has not yet successfully inserted a record in my database.

Now, the question is : How will I execute the rendering of my form element after db_insert had successfully inserted a record in my table?

UPDATE 1:

I tried Shabir A. 's  suggestion:

$save = db_insert('studidtemplates',array('target' =&gt; 'import')) 
    -&gt;fields(array(         
        'template_name' =&gt; 'WEW',            
        'date_created' =&gt; date("Y-m-d H:i:s", time()),
        'user_name' =&gt; $user-&gt;name
    ))       
    -&gt;execute();

watchdog('myid', $save);

if($save){
    return array(
        '#type' =&gt; 'ajax',
        '#commands' =&gt; array(
            ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),                         
        )
    );  
}


But the problem still occurs, When I view my watchdog table, the $save variable has a value of 371. That means it was inserted successfully, as what Shabir A. said, but the table display was not updated. Is there anything wrong with my query? Where am I missing?
You can simply check against inserted id. If insert successful means you get inserted id only then you render the data otherwise do nothing

$inserted_id = db_insert('studidtemplates',array('target' =&gt; 'import')) 
        -&gt;fields(array(         
            'template_name' =&gt; 'WEW',            
            'date_created' =&gt; date("Y-m-d H:i:s", time()),
            'user_name' =&gt; $user-&gt;name
        ))       
        -&gt;execute();
if($inserted_id) {
   return array(
    '#type' =&gt; 'ajax',
    '#commands' =&gt; array(            
        ajax_command_replace('#myid_templates_editor_table_wrapper',     render($form['myid_templates_editor_table_wrapper'])),  

    )
); 
}