Select option in Drupal form API I've created a drupal custom form. I've used a select option for bank which get its value from the bank table. I want to store bankID in bankbranch table and which is a foreign key and display bank name in select box but it shows me an error: 


  Undefined property: stdClass::$bankID in bankbranch_form() (line 40 of
  C:\wamp\www\emp\sites\all\modules\bankbranch\bankbranch.module).


My code is below:

/**
* Implements hook_permission().
*/
function bankbranch_permission() {
  return array(
    'submit bankbranch' =&gt; array(
      'title' =&gt; t('submit bank branch'),
      'description' =&gt; t('submit bank branch form'),
    ),
  );
}

/**
* Implements hook_menu().
*/
function bankbranch_menu(){
  $items = array();
  $items['bankbranch'] = array(
    'title' =&gt; 'Bank Branch Entry Form',
    'type' =&gt; MENU_NORMAL_ITEM,
    'access arguments' =&gt; array('submit bankbranch'),
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('bankbranch_form'),
  );
  return $items;
}

/**
* Bank Branch Form
*/
function bankbranch_form($form, &amp;$form_state){
  $query_bank = db_select('bank', $alias = NULL)
    -&gt; fields($alias = NULL, array('bankID', 'bankname'))
    -&gt; execute()
    -&gt; fetchAll();
  $options_bank = array();
  foreach($query_bank as $value) {
    //bankID as key 
    *$options_bank[$value-&gt;bankID] = t($value-&gt;bankname);* /** line for which it shows notice*/
  }
  $form['bankID'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Select Bank Name'),
    '#options' =&gt; $options_bank,
    '#size' =&gt; 1,
    '#maxlength' =&gt; 128,
    '#required' =&gt; TRUE,
  );
  $form['branchName'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Branch Name'),
    '#size' =&gt; 50,
    '#maxlength' =&gt; 128,
    '#required' =&gt; TRUE,
  );
  $form['branchaddress'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Branch Address'),
    '#size' =&gt; 50,
    '#maxlength' =&gt; 128,
    '#required' =&gt; TRUE,
  );
  $form['branchcode'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Branch Code'),
    '#size' =&gt; 50,
    '#maxlength' =&gt; 128,
    '#required' =&gt; TRUE,
  );
  $form['ifsccode'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Branch IFSC code'),
    '#size' =&gt; 50,
    '#maxlength' =&gt; 128,
    '#required' =&gt; TRUE,
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add item'),
  );
  return $form;
}

/**
* Submit handler for the bank_form. Insert entry into the database.
*/
function bankbranch_form_submit($form, &amp;$form_state){
  $branchID = db_insert('bankbranch')
    -&gt;fields(array(
      'bankID' =&gt; $form_state['values']['bankID'],
      'brachName' =&gt; $form_state['values']['brachName'],
      'branchaddress' =&gt; $form_state['values']['branchaddress'],
      'branchcode' =&gt; $form_state['values']['branchcode'],
      'ifsccode' =&gt; $form_state['values']['ifsccode'],   
    ))
    -&gt;execute();
    drupal_set_message(t('Your form entry has been added'));
}


Any suggestions?
Your lacking some code.

Try changing your code above with:

 $query_bank = db_select('bank', 't1',array('target' =&gt; 'import')); 
 $result = $query_bank
   -&gt;fields('t1',array('bankID','bankname'))                                   
   -&gt;execute(); 

 foreach($result as $value){
      *$options_bank[$value-&gt;bankID] = t($value-&gt;bankname);
 }


The import keyword above is your database settings. It must be declared in your settings.php. Assuming  you are using an external database where your bank  table is located, It must look like the code below:

$databases = array (
    'default' =&gt; 
        array (
            'default' =&gt; 
                array (
                    'database' =&gt; 'Name of Your Drupal Database',
                    'username' =&gt; 'Username of Your Drupal Database',
                    'password' =&gt; 'Password of your Drupal Database',
                    'host' =&gt; 'Host of Your Drupal Database',
                    'port' =&gt; '',
                    'driver' =&gt; 'Driver of Your Drupal Databas', //It can be mysql/pgsql depends whether you are using Postgres or Mysql
                    'prefix' =&gt; '',
                ),
            'import' =&gt;  // This Part is where your database that contains `bank` table should be declared.
                array (
                    'database' =&gt; '',   //Fill it up according to your database settings.
                    'username' =&gt; '',
                    'password' =&gt; '',
                    'host' =&gt; '',
                    'port' =&gt; '',
                    'driver' =&gt; '',
                    'prefix' =&gt; '',
                ),
       ),
);


For more clarification, please refer to this link.