[development] Saving to database

Tolga tolga at ozses.net
Wed Jun 8 19:37:11 UTC 2011

Hello Michael,

Yes, I've checked the value of $form_state['values']['tcmb_currency] and 
it always came back 0 and therefore saved a 0. I found out why thanks to 
#drupal-support on Freenode. All I had to do was replace '#value' => 
'tcmb_currency' from tcmb_settings_form with '#default_value' => 
variable_get('currency',$form_state['values']['tcmb_currency']); and 
then it started saving properly.


08-06-2011 18:18, Michael Favia yazmış:
> On 06/08/11 01:37, Tolga wrote:
>> 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.
> You are correct that the above code sets the variable 'tcmb_currency' 
> (thank you for properly namespacing your variables) to the value of 
> $form_state['values']['tcmb_currency']. Furthermore the variable is 
> passed 'by value' to the variable_set function so it is not modified 
> in anyway in its local scope.
> Have you checked the value of your $form_state['values'] with a dsm()? 
> or print_r() to make sure they are ever properly populated. I noticed 
> you have a validation function and you CAN alter your forms values 
> there so you might want to make sure you arent accidentally unsetting 
> them in some fashion in there. Good luck and please let us know how it 
> goes.

More information about the development mailing list