How to retrieve form entry for use as default value? I'm building a form in a custom module, and I'd like to have that form show as it's default value the value that had been saved to the DB during the previous edit, but I have no idea how to do it.

Here is an example of one of my fields:

$form['savings_ages']['savings_age21'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('21'),
'#size' =&gt; 10,
'#maxlength' =&gt; 10,
'#default_value' =&gt; $whatgoeshere?,
);


My form is the basis for a calculator, so I've got over 80 fields and only ever expect to have 1 row's worth of information in my table.

I have no idea how to pull that out, so any guidance would be welcome. PLease let me know if I need to provide more detail.

EDIT:
I'm still stuck on this, my normal query works when I test in the sql window of PHPmyadmin:

SELECT `factors_age21` FROM `savingfactors` WHERE 1 limit 1


but I can't seem to convert it to the correct format to work with db_query function and echo it out as the default value of my form field. PLus I'll still have to make this query 44 times (I have 44 columns) so if there is a more efficient way, I'd appreciate it.

Second Edit:
I'm starting to get a partial result, but it's still wrong.

$sql = "SELECT factors_age21 FROM {savingfactors} WHERE factors_age21 LIMIT 1";
$res = db_query($sql);
$item = db_fetch_object($res);

//later
$form['savings_factors']['factors_age21'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('21'),
'#size' =&gt; 10,
'#maxlength' =&gt; 10,
'#default_value' =&gt; print $item,
);


It IS producing a result, but it's showing me the number "1", when my correct value is "14"
any help is appreciated.

Thanks
Stephanie
$form['savings_ages']['savings_age21'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('21'),
'#size' =&gt; 10,
'#maxlength' =&gt; 10,
'#default_value' =&gt; $whatgoeshere?,
);


I guess $form['savings_ages'] is a fieldset. If so, then you can use the following code to get the default value that was saved in database during previous edit.

Explanation

When you save a form, a variable gets created with the id that you use to create form element. In your case variable_get('savings_age21', '') will fetch the previously saved value.

Documentation variable_get

Edit

$form['savings_ages']['savings_age21'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('21'),
    '#size' =&gt; 10,
    '#maxlength' =&gt; 10,
    '#default_value' =&gt; variable_get('savings_age21', ''),
);

return system_settings_form($form);


Sorry, I forgot about system_settings_form($form), this way when you save the form a variable with id of form element will get created, later you can retrieve it using variable_get().

If you do not wish to use system_settings_form($form), then you can use variable_set() to save a variable with any name. You have to do this variable_set() inside the submit handler of the form. Later while displaying the form, use the same logic variable_get() to retrieve the variable.