1

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' => array(
      'title' => t('submit bank branch'),
      'description' => t('submit bank branch form'),
    ),
  );
}

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

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

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

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

Any suggestions?

1 Answer 1

1

Your lacking some code.

Try changing your code above with:

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

 foreach($result as $value){
      *$options_bank[$value->bankID] = t($value->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' => 
        array (
            'default' => 
                array (
                    'database' => 'Name of Your Drupal Database',
                    'username' => 'Username of Your Drupal Database',
                    'password' => 'Password of your Drupal Database',
                    'host' => 'Host of Your Drupal Database',
                    'port' => '',
                    'driver' => 'Driver of Your Drupal Databas', //It can be mysql/pgsql depends whether you are using Postgres or Mysql
                    'prefix' => '',
                ),
            'import' =>  // This Part is where your database that contains `bank` table should be declared.
                array (
                    'database' => '',   //Fill it up according to your database settings.
                    'username' => '',
                    'password' => '',
                    'host' => '',
                    'port' => '',
                    'driver' => '',
                    'prefix' => '',
                ),
       ),
);

For more clarification, please refer to this link.

4
  • Thanks for reply. I have created bank table in same default database. After chaining the code (replacing "import" with "default"), it shows same notice as previous. when I replace $options_bank[$value->bankID] = t($value->bankname) with $options_bank[] = t($value->bankname), it shows me the bank names in select box. But the problem is that i want ID as storing value of select box
    – Saurabh
    Aug 12, 2015 at 9:58
  • @Saurabh --> Are you sure bankID does exist? Please recheck your table field if it is misspelled. ALso take note that the database fields are case-sensitive. The value of $value->bankID is undefined, meaning ,either bankID's is really undefine in the database or bankID doesnt really exist. Aug 13, 2015 at 1:23
  • -> I have checked, bankID exist in the bank table and it is as same case(bankID) in the code,but i am really surprised why it did not get the bankID value while bankname is properly working for that
    – Saurabh
    Aug 13, 2015 at 5:23
  • Any suggestions?
    – Saurabh
    Sep 4, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.