Ok, I'm using user_save to update the email address field

$array = array('mail' => $emailfield);
user_save($uid, $array);

I keep getting this error. I have no idea why it's trying to create a new account with the $uid, since that's the user I'm specifying I want the email field to change.

user warning: Duplicate entry '' for key 2 query: INSERT INTO users (mail, created, access) VALUES ('test@theemailtest.com', 1240069427, 1240069427) in /Applications/MAMP/htdocs/aurum/modules/user/user.module on line 327.

Any help would be greatly appreciated!

Comments

rampar’s picture

user_save's UPDATE just doesn't work, it might be a bug or something, I don't know.

Anyway, if you want to do it, just use your own UPDATE

<?php
$updateemail = db_query("UPDATE {users} SET mail='$emailfield' WHERE uid = '$uid'");
?>
wheelercreek’s picture

I'm getting the same error message, although for me the save function is working. It just seems to be trying to save twice. Unfortunately I can't save directly to the user table, we're in a many-site environment with a shared users table.

BTW - I think you're code is incorrect. Rather than pass in the uid, you need to pass in the user object itself, like so:

$usr = user_load(array('name' => $edit['name'])); 
user_save($usr, array('picture'=> $edit['name'].'.jpg'));

For me it's working, but I need someway to kill the error message?

wheelercreek’s picture

I was implementing the hook_user in custom module, and I discovered that in the case the $op is set to "edit" - then user_save() is automatically called behind the scenes. So no need to call that function in the edit case. Simply adding the extra custom user fields to the $edit variable of the hook_user method will suffice.

Also, be aware that with user_save(), if it finds a $uid it automatically attempts to run the "edit" case of any implementation of hook_user(). So attempting to run user_save() inside the "insert" case will also run the "edit" case. This happens because the real insert seems to be performed before the case "insert" of a hook_user method, thus a $uid is already created.

My finished code:

/**
* Implementation of hook_user().
* hook into the user registration process & add a form checkbox for permission regarding pictures
* http://api.drupal.org/api/function/hook_user/6
**/
function mymodule_user($op, &$edit, &$account)
{
  switch($op)
  {
    case 'register':
       $form['allow_img'] = array(
          '#type' => 'checkbox',
          '#title' => t('Please include my picture in User search'),
         ); 
         return $form;
         break;
     case 'form':
        // see if picture is listed
       $usr = user_load(array('name' => $edit['name']));
       $attr = (!empty($usr->picture)) ? array('checked'=>'checked'):'array()';
       $form['allow_img'] = array(
           '#type' => 'checkbox',
           '#title' => t('Please include my picture in User search'),
           '#attributes' => $attr,
        ); 
        return $form;
        break;
      case 'insert':
         //update will be run here, through the user_save().  
        if (!empty($edit['allow_img']))
        {
           user_save($account, array('picture'=>$edit['name'].'.jpg','allow_img'=>1));
        }
        break;
      case 'update':
         // in this case user_save() is automatically called after whatever we add to the $edit array.  
         // So don't call it here, causes error. 
         if (!empty($edit['allow_img']))
             $edit['picture'] = $edit['name'].'.jpg';
         else
             $edit['picture'] = ''; 
         break;
    }
}