49
$dbh->prepare("INSERT INTO pc_message (id,thread_id,parent_id,
thread_pos,posted_on,level,author,subject,body)
VALUES (?,?,?,?,NOW(),?,?,?,?)");
$dbh->execute($prh,array($id,$thread_id,$parent_id,$thread_pos,$level,
$_REQUEST['author'],$_REQUEST['subject'],
$_REQUEST['body']));
// Tell MySQL that others can use the pc_message table now
$dbh->query('UNLOCK TABLES');
}
// pc_message_list() displays a list of all messages
function pc_message_list() {
global $dbh;
print '<h2>Message List</h2><p>';
/* order the messages by their thread (thread_id) and their position
within the thread (thread_pos) */
$sth = $dbh->query("SELECT id,author,subject,LENGTH(body) AS
body_length,
posted_on,level FROM pc_message
ORDER BY thread_id,thread_pos");
while ($row = $sth->fetchRow()) {
// indent messages with level > 0
print str_repeat(' ,4 * $row->level);
// print out information about the message with a link to read it
print<<<_HTML_
<a href="$_SERVER[PHP_SELF]?cmd=read&id=$row->id">$row->subject</a> by
$row->author @ $row->posted_on ($row->body_length bytes)
<br>
_HTML_;
}
// provide a way to post a non-reply message
printf('<hr><a href="%s?cmd=post">Start a New Thread</a>',
$_SERVER['PHP_SELF']);
}
// pc_message_read() displays an individual message
function pc_message_read() {
global $dbh;
/* make sure the message id we're passed is an integer and really
represents a message */
$id = intval($_REQUEST['id']) or die("Bad message id");
if (! ($msg = $dbh->getRow(
"SELECT author,subject,body,posted_on FROM pc_message WHERE id =
$id"))) {
die("Bad message id");
}
/* don't display user-entered HTML, but display newlines as
HTML line breaks */
$body = nl2br(strip_tags($msg->body));
// display the message with links to reply and return to the message
list