By finke77 on
Hello!
I am trying to develop a module which imports users and I am using the save user function.
When I execute a own function it is executed several times.
My code looks as follows:
function mytest() {
$user_data = array(
'is_new' => TRUE,
'status' => 1,
'mail' => 'mail@domain.dom',
'init' => 'mail@domain.dom',
'name' => 'testuser',
'pass' => 'test',
'roles' => array( 2=>TRUE, 4=>TRUE),
'language' => 'de',
'contact' => 1,
'messaging_default' => 'mail',
'notifications_send_interval' => 0,
'notifications_auto' => 1,
'created' => time(),
);
// create user
$local_user = user_save( '', $user_data );
return "Created user ".$local_user->name." with UID: ".$local_user->uid." and Email: ".$local_user->mail."<br>";
}
I called it with the devel module => execute php:
require_once("sites/all/modules/cfimport/import_user.inc");
mytest();
And I get the following error message.
* user warning: Duplicate entry 'testuser' for key 2 query: INSERT INTO users (status, mail, init, name, pass, language, created, access) VALUES (1, 'mail@domain.dom', 'mail@domain.dom', 'testuser', '098f6bcd4621d373cade4e832627b4f6', 'de', 1238067129, 1238067129) in /var/www/drupal-test/html/modules/user/user.module on line 327.
* user warning: Duplicate entry 'testuser' for key 2 query: INSERT INTO users (status, mail, init, name, pass, language, created, access) VALUES (1, 'mail@domain.dom', 'mail@domain.dom', 'testuser', '098f6bcd4621d373cade4e832627b4f6', 'de', 1238067129, 1238067129) in /var/www/drupal-test/html/modules/user/user.module on line 327.
* user warning: Duplicate entry 'testuser' for key 2 query: INSERT INTO users (status, mail, init, name, pass, language, created, access) VALUES (1, 'mail@domain.dom', 'mail@domain.dom', 'testuser', '098f6bcd4621d373cade4e832627b4f6', 'de', 1238067129, 1238067129) in /var/www/drupal-test/html/modules/user/user.module on line 327.
* user warning: Duplicate entry 'testuser' for key 2 query: INSERT INTO users (status, mail, init, name, pass, language, created, access) VALUES (1, 'mail@domain.dom', 'mail@domain.dom', 'testuser', '098f6bcd4621d373cade4e832627b4f6', 'de', 1238067130, 1238067130) in /var/www/drupal-test/html/modules/user/user.module on line 327.
Does anyone know why this happens and how I can correct it?
Thanks in advance,
Christian
Comments
For anyone else who might
For anyone else who might stumble across this... (I know it's an old thread)
The first parameter to user_save() is expected to be an object (the $user object). In the case where you're making a new user, you should still supply a blank object. e.g.,
That should fix the above problem (i.e., the 'Duplicate entry' warning messages).
Kendall