[development] Saving to database

Michael Favia michael.favia at gmail.com
Wed Jun 8 15:18:20 UTC 2011

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.

Michael Favia                   michael at favish.com
tel. 512.669.5650            http://www.favish.com

More information about the development mailing list