Modify a variable before it saves to database I have a user profile form.  I'm having trouble modifying a variable before it saves to the database.  I have these hooks in my template.php but the value going into my database is still the same value that was submitted in the form post.

function mytheme_user_profile_form_presave()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' =&gt; array(0 =&gt; array('value' =&gt; $foo))),$form_state);

    }
  }
    return '';
}

function mytheme_preprocess_user_profile_form()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' =&gt; array(0 =&gt; array('value' =&gt; $foo))),$form_state);

    }
  }
    return '';
}

function mytheme_user_profile_form_validate()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']-&gt;field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' =&gt; array(0 =&gt; array('value' =&gt; $foo))),$form_state);

    }
  }
    return '';
}


what am i doing wrong?

UPDATE

Ok, I added this in my template.php

function mytheme_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form['#id'] == 'user-profile-form') {
    $form['#validate'][] = 'mymodule_foo_data';
  }
}


Then I added this to mymodule.module

function mymodule_foo_data(&amp;$form,&amp;$form_state)
{

        $form['field_last_name']['und'][0]['value'] = 'foo';
}


The function mymodule_foo_data gets fired on save.  But the value foo doesn't get written to my database.  Instead, the value that's in the $_POST array is what gets written to the database.  how do I get foo into the databasE?
I don't think your approach is the best. You need to do the following: 


Add a custom module.
Implement hook_form_alter in your module and and alter the user
profile form. Form id: user-profile-form.
Add a validation function i. e. $form['#validate'][] =
'my_module_custom_form_validate';.
add an implementation for the function in your module and modify the form values as you wish.


Also, you don't need to use the $_POST variable if you are following the drupal form api.