[development] Saving to database

Tolga tolga at ozses.net
Wed Jun 8 06:37:35 UTC 2011


Hi,

I have the below code, and it doesn't quite do what I want.

function tcmb_settings_form_submit($form, $form_state) {
   variable_set('tcmb_currency', $form_state['values']['tcmb_currency']);
   $sql = "INSERT INTO {tcmb} (currency) 
VALUES('".$form_state['values']['tcmb_currency']."')";
   return db_query($sql);
}

According to a Lullabot video I watched, the variable_set function 
should set the posted value to variables table, and the field should 
stay with the correct value in it, but it automatically goes back to the 
first value. Also, stupid question but how do you save to database. The 
above code always saves a 0.

I'm specifying my field like below:

function tcmb_settings_form() {
   $form = array();
   $form['tcmb_currency'] = array(
     '#title' => t('Input the currency code for the rates'),
     '#value' => 'currency',
     '#type' => 'select',
     '#options' => array('0' => 'USD', '1' => 'CAD', '2' => 'XDR', '3' 
=> 'DKK', '4' => 'SEK', '5' => 'CHF', '6' => 'NOK', '7' => 'JPY', '8' => 
'SAR', '9' => 'KWD', '10' => 'AUD', '11' => 'EUR', '12' => 'GBP', '13' 
=> 'RUB', '14' => 'RON', '15' => 'IRR', '16' => 'BGN', '17' => 'DEM', 
'18' => 'BEF', '19' => 'LUF', '20' => 'ESP', '21' => 'FRF', '22' => 
'IEP', '23' => 'ITL', '24' => 'NLG', '25' => 'ATS', '26' => 'PTE', '27' 
=> 'FIM', '28' => 'GRD'),
     );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
     );
   if (empty($form_state['storage']['new_tcmb_currency'])) {
       $form['tcmb_new_tcmb_currency'] = array(
         '#type' => 'submit',
         '#value' => t('Add another currency'),
         '#validate' => array('tcmb_settings_form_validate'),
         );
       }
     return $form;
}

If you could help me with the above, I'd appreciate a lot. Thanks,
Tolga


More information about the development mailing list