Search code examples
phpsqlphpbbdbal

PhP SQL Query Using Dbal and PhPBB Functions


Alright, here I am again trying to write code from scratch and I can't get it exactly right... All I want to do is retrieve the group id that the user is in (excluding registered users) and then use that in the following statements: If the retrieved group_id is 10 then return specified message else remove the user from the retrieved group_id and place them into group_id 10. This is what I have so far, but I think I've made an error somewhere in the dbal... and as for the user group add/del functions, I'm not sure I'm using them right... also I included the functions_user.php but wasn't sure I really needed to or if I placed it correctly. Well here's what I've got, any help?

    $integer = 2;

    $sql = 'SELECT group_id FROM ' . USER_GROUP_TABLE . '
            WHERE user_id = ' . (int) $user->data['user_id'] . "
            AND group_id != '" . (int) $integer . "'";
    $result = $db->sql_query($sql);

    if ($result == 10)
    {
       $message = sprintf($user->lang['CANNOT_USE_TRAVEL_ITEM'], $this->data['name']);
    }
    else
    {
       include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

       $userid = $user->data['user_id'];

       group_user_add((10), array($user_id));
       group_user_del(($result), array($user_id));

       $message = sprintf($user->lang['TRAVEL_ITEM_NOW_USE'], $this->data['name']);
    }

Solution

  • Alright, I got it working... here is the code. Used the group_memberships function to both eliminate the SQL query as well as providing the correct boolean result to the group_user_add and group_user_del functions.

        global $user, $shop, $db, $phpEx, $phpbb_root_path;
    
        $this->remove_item();
    
        include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
    
        if (group_memberships(10,$user->data['user_id'],true))
        {
           $message = sprintf($user->lang['CANNOT_USE_TRAVEL_ITEM'], $this->data['name']);
        }
        else
        {
           $groups = group_memberships(false,$user->data['user_id']);
           foreach ($groups as $grouprec)
           {
                   $groupid = $grouprec['group_id'];
           }
    
           group_user_add((10), array($user->data['user_id']));
           group_user_del(($groupid), array($user->data['user_id']));
    
           $message = sprintf($user->lang['TRAVEL_ITEM_NOW_USE'], $this->data['name']);
        }
    
        return $message;