<?php


// contents of function UpdateSubject

/*

Copyright © 2001 Nick Gammon.

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, 
  or (at your option) any later version. 

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  See the GNU General Public License for more details. 

*/
  
  $query = "SELECT * FROM bbpost "
         . "WHERE bbsubject_id = $bbsubject_id "
         . "ORDER BY post_date DESC LIMIT 1";
   
//  echo $query;
                        
  $result = mysql_query ($query) 
      or Problem ("Select of latest post failed: " . mysql_error ());
    
  if (($row = mysql_fetch_array ($result)))
    {
    $last_date = "'" . $row ['post_date'] . "'";
    $last_user = $row ['bbuser_id'];
    }
  else
    {
    $last_date = "NULL";
    $last_user = "NULL";
    }
    
  mysql_free_result ($result);

// work out how many posts in the subject

  $result = mysql_query ("SELECT count(*) AS count_posts FROM bbpost "
                       . "WHERE bbsubject_id = $bbsubject_id") 
      or Problem ("Select of count of posts failed: " . mysql_error ());
    
  if (($row = mysql_fetch_array ($result)))
    $count_posts = $row ['count_posts'];
  else
    $count_posts = "NULL";
    
  mysql_free_result ($result);
      
  $query = "UPDATE bbsubject SET "
         . "  last_post_date = $last_date, "
         . "  last_post_user = $last_user, "
         . "  count_posts = $count_posts "
         . "WHERE bbsubject_id = $bbsubject_id";
  
  $result = mysql_query ($query)
    or Problem ("Update of bbsubject failed: " . mysql_error ());

?>
