0

This question already has an answer here:

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?

marked as duplicate by DRUPWAY, Yusef Mohamadi, Shawn Conn, mradcliffe, Neograph734 Mar 26 '17 at 12:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

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.

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