I was looking to update the user programmatically and could not find anything in Drupal.org. But stumbled upon the article which had it solved. I thought of adding the reference here for others..

Here's a quick code snippet I use to programmatically create new users:

$newUser = array(
  'name' => 'username',
  'pass' => 'password', // note: do not md5 the password
  'mail' => 'email address',
  'status' => 1,
  'init' => 'email address'
user_save(null, $newUser);

And, here's how you can update an existing user:

// load user object
$existingUser = user_load('USERID');

// update some user property
$existingUser->some_property = 'blah';

// save existing user
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);

If you wanted to update an existing user's profile data:

// load user object
$existingUser = user_load('USERID');

// create an array of properties to update
$edit = array(
  'profile_first_name' => 'Eric'

// save existing user
  (object) array('uid' => $existingUser->uid),
  'Personal Information' // category

Thanks to Eric.London Programmatically add/update users using user_save()


Begun’s picture

If you are updating an existing user's password programmatically do you need to hash the password? There is a drupal function for hashing passwords ( user_hash_password() ), but is it required when doing user_save on an existing user?

batigolix’s picture

To change the password of an existing user:

$edit['pass'] = 'letmein';
user_save($user, $edit);


gperez’s picture

Hi. If I do this, somehow the user-role if being deleted. Why?!

$existingUser = user_load($user_uid);

// this corresponds to POST data
$form_fields = array(
			'field_title' 			 => array('name' => 'Title','value' => $field_title),
			'field_first_name' 			 => array('name' => 'First Name','value' => $field_first_name),
			'field_last_name' 			 => array('name' => 'Last Name','value' => $field_last_name),
			'field_phone'		 => array('name' => 'Phone','value' => $field_phone),			
			'field_company'		 => array('name' => 'Company','value' => $field_company)

// new data
foreach ($form_fields as $key => $field){
         $newData[$key] = $field['value'];

// save
user_save((object) array('uid' => $existingUser->uid), (array) $newData, 'Personal Information');
gperez’s picture

Nevermind. I had add the lang array folds and now works fine:

 if($key != 'mail')
   $existingUser->{$key}['und'][0]['value'] = $field['value']; 
   $existingUser->{$key} = $field['value']; // core field
TheBarnacle’s picture


I'm trying to use your method above, for updating an existing user in the hook_user_insert hook.

function mymodule_user_insert(&$edit, $account, $category) {


user_save((object) array('uid' => $account->uid), (array) $account);

But it fails with an integrity constraint:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '38' for key 'PRIMARY': INSERT INTO {users} (uid, name, pass, mail, theme, signature, signature_format, created, access, login, status, timezone, language, picture, init, data) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15); Array ( [:db_insert_placeholder_0] => 38 [:db_insert_placeholder_1] => wftest02 [:db_insert_placeholder_2] => $S$DD2J4Le0C82VZpGUCUY8jsleOzUapwCLCSlfb4ThNlfnbCQOONWY [:db_insert_placeholder_3] => wftest02@wf.com [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => full_html [:db_insert_placeholder_7] => 1415725599 [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 1 [:db_insert_placeholder_11] => Europe/London [:db_insert_placeholder_12] => [:db_insert_placeholder_13] => 0 [:db_insert_placeholder_14] => wftest02@wf.com [:db_insert_placeholder_15] => a:5:{s:16:"ckeditor_default";s:1:"t";s:20:"ckeditor_show_toggle";s:1:"t";s:14:"ckeditor_width";s:4:"100%";s:13:"ckeditor_lang";s:2:"en";s:18:"ckeditor_auto_lang";s:1:"t";} ) in drupal_write_record() (line 7202 of /home/whitefusedev/domains/rstmh.whitefusedev.co.uk/public_html/includes/common.inc).

I don't see how what I'm doing is different from what you suggested. Do you have any idea why it might not be working for me?

TheBarnacle’s picture

For the record, I fixed this in the end by changing $account->is_new to 0.

suvzz’s picture

@shariharan appreciate for the time taken out by you to post it here :)

prsnjtbarman’s picture

second example works fine for existing user

jatbhatti’s picture

$existingUser = user_load($uid);
$existingUser ->field_Your_field_name['und'][0]['value'] = "somevalue";
user_save($existingUser );