I need to Create a new user programmatically. So, I have used user_save method, as shown here:

$usersave = array(
'name' => 'abc',
'pass' => 'abc',
'mail' => 'abc@gmail.com',
'status' => 1,
'roles' => array(
  DRUPAL_AUTHENTICATED_RID => 'authenticated user',
      5 => 'customer'

$save = user_save($usersave);

But it throws this error message:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY': INSERT INTO {users} (name, pass, mail, status) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => abc [:db_insert_placeholder_1] => abc [:db_insert_placeholder_2] => abc@gmail.com [:db_insert_placeholder_3] => 1 ) in drupal_write_record() (line 7383 of /home/vijay/public_html/vssb/public/includes/common.inc).

What's the issue?


This is a duplicate of Create a user programmatically and give they a role

// The first parameter should be blank so a new user is created.
user_save('', $new_user);

If you want to use user_save($new_user); you can set $new_user->is_new to TRUE or omit the $account->uid field.

| improve this answer | |

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