What is the best practice for updating a timestamp in a MySQL table every time a new post is made in a forum thread?

To update a timestamp in a MySQL table every time a new post is made in a forum thread, you can use a trigger in MySQL. By creating a trigger that updates the timestamp column whenever a new post is inserted into the table, you can ensure that the timestamp is always up to date with the latest post.

// Create a trigger in MySQL to update the timestamp column in the forum thread table
$query = "CREATE TRIGGER update_timestamp
AFTER INSERT ON posts
FOR EACH ROW
BEGIN
    UPDATE forum_threads
    SET last_post_timestamp = NOW()
    WHERE thread_id = NEW.thread_id;
END";

// Execute the query
mysqli_query($connection, $query);