how to do validation for duplicate primary key in database I want to alert user for duplicate primary key insertion in database. here is the code:



&lt;?php

function formModule_menu() {
  $items = array();

  $items['form/formModule'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' =&gt; 'Form', //page title
    'description' =&gt; 'A form with a text field and Submit button.',
    'page callback' =&gt; 'formModule_intro', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form

    'access callback' =&gt; TRUE,
    'expanded' =&gt; TRUE,
  );

  $items['form/formModule/form1'] = array(
    'title' =&gt; 'Form One',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('formModule_form'),
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_NORMAL_ITEM,
    'weight' =&gt; 1,
  );


  return $items;
}


function formModule_form($form, &amp;$form_state) {

 $result = db_query("SELECT n.nid, n.title FROM {node} n 
                         WHERE n.type = 'product'");

    $options = array();
   foreach($result as $row) {
      $options[$row-&gt;nid] = $row-&gt;title;
    }
    //dsm($options);



    $form['productid'] = array(
    '#type' =&gt; 'select', 
    '#title' =&gt; t('Select a product'), 
    '#required' =&gt; TRUE,
    '#options' =&gt; $options,
    '#description' =&gt; t('Choose an option.'),
  );


  $form['stock'] = array(
    '#type' =&gt; 'textfield', //you can find a list of available types in the form api
    '#title' =&gt; 'Enter the stock',
    '#size' =&gt; 10,
    '#maxlength' =&gt; 10,
    '#description' =&gt; t('stock should be integer value'),
    '#required' =&gt; TRUE, //make this field required
  );




  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'submit',
  );

  return $form;
}


function formModule_form_validate($form, &amp;$form_state) {

if (!($form_state['values']['stock'] &gt; 0)){
   form_set_error('stock', t('Stock must be a integer value only.'));
  }

}

function formModule_form_submit($form, &amp;$form_state) {

global $user;

  // Here u can insert Your custom form values into your custom table.
  db_insert('formModule_stock')
    -&gt;fields(array(
      'productid' =&gt; $form_state['values']['productid'],
      'stock' =&gt; $form_state['values']['stock'],
      'created' =&gt; REQUEST_TIME,

    ))-&gt;execute();
    drupal_set_message("successfully saved formModule_stock"); 


}
What you are doing is:


Show a "select" form element containing a list of nodes whose content type is "product"
Show an error if there is a row in a database about the node the user selected


That is poor UX.
Suppose that there are 20 nodes of that content type, and only two nodes don't have a row in the database table where you save the data; there is a high probability the user gets the error message about the data being already present at least two times. Imagine what would happen when all the nodes have a corresponding row in the other database table. 

What you need to do is showing only the nodes for which there isn't a row in the other database table. That means, for example, using a query like the following one.

$result = db_query("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'product' AND n.nid NOT IN (SELECT s.productid from {formModule_stock} s)");


The module should also show a message in the case all the nodes have a row in the other database table. In this case, the user should not see the form to select a node, rather than seeing a "select" form without any item.

As side note, avoid using upper case letters in the short name of modules. While PHP is case insensitive, you would have problems when you write update functions for your module.