How to check term ID present in database table before updating I need to increment a counter on every taxonomy page load. I am adding the code in taxonomy-term.tpl.php file in my theme. I need to add a new entry if the term is not present and need to increment the 'totalview' if its already present. Here is my code, can any one help me to add the condition.

db_insert('custom_file_download_count')
-&gt;fields(array(
  'totalview' =&gt; 1,
  'tid' =&gt; arg(2),
  'timestamp' =&gt; REQUEST_TIME,
  'uid' =&gt; $GLOBALS['user']-&gt;uid,
))

db_update('custom_file_download_count')
-&gt;fields(array(
  'totalview' =&gt; 1,
  'tid' =&gt; arg(2),
  'timestamp' =&gt; REQUEST_TIME,
  'uid' =&gt; $GLOBALS['user']-&gt;uid,
))
-&gt;expression('totalview', 'totalview + 1')
-&gt;execute();
Your compound key you need to check for is for uid &amp; tid. You can check for a row's existance (thus, need to insert) with a db_query function call &amp; rowCount method call.

if(db_query('SELECT totalview FROM {custom_file_download_count} WHERE tid = :tid AND uid = :uid', array(':tid' =&gt; $tid, ':uid' =&gt; $GLOBALS['user']-&gt;uid))-&gt;rowCount() == 0) {
  db_insert('custom_file_download_count')
  -&gt;fields(array(
    'totalview' =&gt; 0,
    'tid' =&gt; arg(2),
    'timestamp' =&gt; REQUEST_TIME,
    'uid' =&gt; $GLOBALS['user']-&gt;uid,
  ))-&gt;execute();
}
db_update('custom_file_download_count')
-&gt;fields(array(
  'totalview' =&gt; 1,
  'tid' =&gt; arg(2),
  'timestamp' =&gt; REQUEST_TIME,
  'uid' =&gt; $GLOBALS['user']-&gt;uid,
))
-&gt;expression('totalview', 'totalview + 1')
-&gt;execute();