How to insert/update data in database
Use db_merge


  In the general sense, a Merge query is a combination of an Insert
  query and an Update query. If a given condition is met, such as a row
  with a given primary key already existing, then an Update query is
  run. If not, an Insert query is run.


Thus, db_merge adds it, if it doesn't exist yet. OR updates it, if it already exists.


db_merge('example')
  -&gt;key(array('name' =&gt; $name))
  -&gt;fields(array(
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ))
  -&gt;execute();

  
  by: dzutaro, tlyngej, stewart.adam, AndreyMaximov