Hello,

I've been developing simple modules in Drupal 6 for some time now, but I have an issue which I really don't understand why it happened.

$update_role = db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $ba_rid);

I also updated my code to...

 $add_role = array('uid' => $uid, 'rid' => $ba_rid);
 drupal_get_schema(FALSE);
 db_lock_table('users_roles');
 $role_add = drupal_write_record('users_roles', $add_role);
 db_unlock_tables();

I have that line of code via function call inside hook_user. The code shall run when a new user is created after signing up a form that gives him special role in the site. I suppose that is during..

switch ($op) {
  case 'insert':
 .
 .
 .
}

However, changes is NOT made in user_roles table and no data is written. I have $uid, $ba_rid printed and those are correct variables with correct values. I also added and printed db_affected_roles and $update_role on another drupal_set_message and it says, 1 and 1, which means 1 row affected and query is successful. However, the changes wasn't made on the table.

But if the same function is called (the function that contains the above codes) when a user has logged in, changes are made, data is written on the DB and the current user has his role changed!

Can anyone help me out? I'm out of basic ideas...

Thanks!

Comments

marashi’s picture

Version: 6.x-dev » 6.20
Status: Active » Fixed

I think there is a conflict somewhere in the core! maybe the "users_roles" table has been locked for direct access or ..., but I found this solution:
instead of trying to insert a new row in "users_roles" table by:

$sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)', db_query($sql, $user->uid, 3);

just let Drupal itself, do it for you by using this code in user('insert') hook:

$your_rid = 3;
$edit['roles'][$your_rid] = $your_rid;

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.