Here is my corect to code that update forum counts. Some condition must be additionaly check becouse when forum have no posts you could see "Update forum error".


// Update forum counts
else if (isset($_POST['update_counts']))
{
  $result = $db->query('select id from '.$db->prefix.'forums where redirect_url is null') or error('Unable to select forum', __FILE__, __LINE__, $db->error());
  while ($cur_forum = $db->fetch_assoc($result))
  {
    $counts = $db->query('select count(id) topics, sum(num_replies) replies from '.$db->prefix.'topics where forum_id='.$cur_forum['id']) or error('Unable to select forum', __FILE__, __LINE__, $db->error());
    $forum_counts = $db->fetch_assoc($counts);

    $db->query('update '.$db->prefix.'forums set num_topics='.$forum_counts['topics'].',num_posts='.($forum_counts['replies']+$forum_counts['topics']).' WHERE id='.$cur_forum['id']) or error('Unable to update forum', __FILE__, __LINE__, $db->error());

    $counts = $db->query('select last_post,last_post_id,last_poster from '.$db->prefix.'topics where forum_id='.$cur_forum['id'].' order by last_post desc limit 1') or error('Unable to select forum', __FILE__, __LINE__, $db->error());
    $forum_counts = $db->fetch_assoc($counts);

    if (($forum_counts['last_post']) && ($forum_counts['last_post_id']) && ($forum_counts['last_poster']))
        $db->query('update '.$db->prefix.'forums set last_post='.$forum_counts['last_post'].',last_post_id='.$forum_counts['last_post_id'].',last_poster="'.$forum_counts['last_poster'].'" WHERE id='.$cur_forum['id']) or error('Unable to update forum', __FILE__, __LINE__, $db->error());
  }

  redirect('admin_forums.php', 'Forum Counts Updated. Redirecting …');
}