使用Dbal和PhPBB函数的PhP SQL查询

问题描述:

好吧,在这里,我再次尝试从头开始编写代码,我无法完全正确地...我想要做的就是检索用户所在的组标识(不包括注册用户),然后在下面的语句中使用它:如果检索到的group_id是10,则返回指定的消息,否则从检索到的group_id中移除用户并将它们放入group_id 10中。这是我迄今为止的内容,但我认为我在dbal的某个地方犯了一个错误...至于用户组add/del函数,我不确定我是否正确使用它们......我还包括了functions_user.php,但不确定我真的需要或如果我正确地放置它。那么这里是我的,有什么帮助?使用Dbal和PhPBB函数的PhP SQL查询

$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']); 
    } 

好吧,我知道它的工作...这里是代码。使用group_memberships函数可以消除SQL查询以及向group_user_add和group_user_del函数提供正确的布尔结果。

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;