Following code not working in our installation of drupal. :

function testform_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'insert':
		$sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)';
		db_query($sql, $user->uid, 3);
	break;
	}
}

The values just will not insert into the database. No error messages, nothing.

If we replace $user->uid with an actual value e.g 'db_query($sql, 124, 3)', the values insert fine.

Have also tried:

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

This does not work either.

However, this works perfectly on a fresh installation of drupal.

Also, if I create another table, e.g. user_role, this table updates fine with the function.

Could there be a module conflict of some kind? Can give a list of modules installed if required.

Comments

freestylegary@yahoo.com’s picture

Status: Active » Fixed

Have isolated the issue to the 'workflow_ng' module. I don't need the module so disabling it fixes it for me. But the question as to why the issue would occur with this module remains...

Status: Fixed » Closed (fixed)

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

marashi’s picture

Version: 5.18 » 6.20

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;