Insert value to field programmatically and remove cache from field I have content type feature and i have field hit_count in this c.type, also i have db table - hit_count - example:

nid   hit_count

1     434
2     233
3     512


I want to output hit_count inside my field - hit_count on current node(also should appear on node edit). So for example if node id is 1, field hit_count = 434, and if admin want to edit this field i want to update field value to my table hit_count with current nid and field value hit_count.

To save value into my database table i will use hook_node_update and just get fields nid and hit_count values and update my table.

But i don't know what hook should i use or how to insert this value from database to my field. There are lots of nodes with content type feature.

UPDATE


  My first thought is - why do you use a separate db table for this rather than simply using an integer field in the content type?


Hit count module was created long time ago, client wanted to disable/enable statistics for every content type, so default statistics module does not fit our needs. And now client want to manage this hit counts for certain nodes etc...

Also i managed to insert value to edit form on hook_exit updated custom hit count module.

function hit_count_exit($destination = NULL) {
    if ($node = menu_get_object()) {
        $selected_types = array_filter(variable_get('hit_count_content_types', array()));
        if (!empty($selected_types)) {
            if (in_array($node-&gt;type, $selected_types)) {
                db_update('node_hit_counter')
                        -&gt;expression('totalcount', 'totalcount + 1')
                        -&gt;condition('nid', $node-&gt;nid)
                        -&gt;execute();
                if($node-&gt;type == 'feature') {
                    $val = db_query("SELECT * FROM {node_hit_counter} WHERE nid='$node-&gt;nid'")-&gt;fetchAll();
                      db_merge('field_data_field_hit_count_node')
                        -&gt;key(array('entity_id' =&gt; $node-&gt;nid))
                        -&gt;fields(array(
                                'entity_type' =&gt; 'node',
                                'bundle' =&gt; 'feature',
                                'deleted' =&gt; 0,
                                'entity_id' =&gt; $node-&gt;nid,
                                'revision_id' =&gt; $node-&gt;nid,
                                'language' =&gt; 'und',
                                'delta' =&gt; 0,
                                'field_hit_count_node_value' =&gt; $val[0]-&gt;totalcount,
                        ))
                        -&gt;execute();
                }

            }
        }
    }
}


But right now i see next problem. I need to get rid of Drupal field cache, cuz i see cached value every time i reload page, and if i clear all caches i get current value of hit_count.
I also think you could use an integer field on the node for this - but keeping that aside, the  problem you are facing (the inserted value not being refreshed by the cache) is caused by the fact that you are trying to write in the database table field_data_field_hit_count_node directly. This is definitely discouraged, and you should be using the proper API instead.

Instead of db_merge('field_data_field_hit_count_node')-&gt;...-&gt;execute() you would want to do something like:

$node-&gt;field_hit_count_node[LANGUAGE_NONE][0]['value'] = $val[0]-&gt;totalcount;
$node-&gt;save();


Doing it this way ensures that the saving is done by Drupal, the caches are cleared as required, hooks as called as necessary, etc. 

In addition if you don't mind using the entity module, then you can do this with Entity Metadata Wrappers in a simpler way:

$nw = entity_metadata_wrapper('node', $node);
$nw-&gt;field_hit_count_node = $val[0]-&gt;totalcount;
$nw-&gt;save();