I made a foreign key in a table(B) with a reference to another table(A) (I want to block a group of users at once, depending on their role instead of blocking them one by one) table(A) is for roles and it contains the role_name(admin etc…) and the role_status(blocked or not) and the table(B) is for users which containes the username, user_role and the user_role_status.
Now If I wanted to block a group of users(editors) I will change the role_status in the roles table and it will replicate to or change the user_role_status in the users table, But the problem is that when I do this it changes the user_role_status for all users with any role and block all of them.
The question is ( is there any way to prevent that and change only the value of the group I want to block ?? something like UPDATE CASCADE with condition (or where user_role in users table = role_name in roles table) or something like that.
ALTER TABLE users ADD CONSTRAINT fk_user_role_status FOREIGN KEY (user_role_status) REFERENCES roles(role_status) ON UPDATE CASCADE;
COMMIT;
/*--------------------------------------------------------------*/
/* Find role status
/*--------------------------------------------------------------*/
function find_by_current_roleStatus($user_role_status)
{
global $db;
$sql = "SELECT user_role_status FROM users WHERE user_role_status = '{$db->escape($user_role_status)}' LIMIT 1";
$result = $db->query($sql);
return($db->num_rows($result) === 0 ? true : false);
}
/*--------------------------------------------------------------*/
/* Function for checking if user role status banned or allowed
/*--------------------------------------------------------------*/
function login_require_roleStatus($require_user_role_status){
global $session;
$current_user = current_user();
$role_status = find_by_current_roleStatus($current_user['user_role_status']);
//if Role status banned
if ($current_user['user_role_status'] === '0'):
$session->msg('d','banned ');
redirect('home.php',false);
//if user group allowed
elseif($current_user['user_role_status'] === '1'):
return true;
endif;
}