Foreign key on update cascade with condition (or where value = value)

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;
     }

Don’t store the current_role_status in your users table.
Create a n:1 relation between users and roles by storing a role_id as foreign key
in your users table.

Table users:

PK	id
FK	role_id
...

Table roles

PK id
   name
   status	
...   

Add a constraint so a role can’t be deleted when still referenced by a user:

 ALTER TABLE users ADD CONSTRAINT fk_user_role FOREIGN KEY (role_id) REFERENCES  roles(id) ON DELETE RESTRICT;

This way whenever you change the status of a role, every user which references this role
will ‘know’ his current_role_status.

SELECT role.status AS current_role_status FROM users JOIN roles ON users.role_id = roles.id WHERE users.id=XX
Sponsor our Newsletter | Privacy Policy | Terms of Service