1

We have a webform and when submitting this form we would like to update user info in the database regarding values of submitted Webform.

I already managed to get the submitted values from the webform in my custom module. But I don't know how to update the user info in the database because all user info are in separated tables.

I can run a few update queries for each value but it's not a logical way for me!

Any other suggestions?

0

1 Answer 1

1

You can equally use either user_save() or field_attach_update().

// Load the user's account.
global $user;
$account = user_load($user->uid);
$edit = $account->field_my_custom_field[LANGUAGE_NONE][0][value] = $new_value;
user_save($account, $edit);
// field_attach_update
$account->field_my_custom_field[LANGUAGE_NONE][0][value] = $new_value;
field_attach_update('user', $account);

The difference between using one function or the other is that user_save() will update all the user fields whereas field_attach_update() will update only the specified field.

4
  • thx for ur answer, just a question, is this line getting the user ID and update it's profile '$account = user_load($user->uid);' ?
    – Sinf
    Commented Aug 18, 2016 at 12:33
  • Welcome! No, you need the $user->uid to user_load a user and then when you user_save to update the user. The uid is a field attached to the $user entity, do a describe users; to your database and you will see all the available user fields. Thus, we are never getting the user id in the way you mean it, as a single/isolated value, instead, we are getting the user object with all its fields attached (from the db) and we decide to which field(s) to refer to.
    – mchar
    Commented Aug 18, 2016 at 13:23
  • Thx @mchar I understood :)
    – Sinf
    Commented Aug 18, 2016 at 13:27
  • @Soheyl Using user_load($user->uid) is just an example. In your code you will use the user ID you have, if you have one, or the code shown in this answer, if you need to save a field for the currently logged-in user.
    – avpaderno
    Commented Aug 21, 2016 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.