I'm currently in the process of coding my own forum with PHP and MySQL, and I've run into a problem when trying to gather the total post count for each user on the topic display page. Here is the source of the showtopic.php page:
<?php
// start the session
session_start();
// IE history fix
header("Cache-control: private");
// check for required info from the query string
if (!$_GET[topic_id]) {
header("Location: topiclist.php");
exit;
}
// connect to server and select database
include 'db/db.php';
// verify the topic exists
$verify_topic = "SELECT topic_title FROM forum_topics WHERE topic_id = $_GET[topic_id]";
$verify_topic_res = mysql_query($verify_topic, $conn) or die(mysql_error());
if (mysql_num_rows($verify_topic_res) < 1) {
// this topic does not exist
$display_block = "<p><em>This topic does not exist. <a href=\"topiclist.php\">Forum Index</a></em></p>";
} else {
// get the topic title
$topic_title = stripslashes(mysql_result($verify_topic_res,0,'topic_title'));
// gather the posts
$get_posts = "SELECT post_id, post_text, date_format(post_create_time, '%b %e %Y at %r') AS fmt_post_create_time, post_owner FROM forum_posts WHERE topic_id = $_GET[topic_id] ORDER BY post_create_time ASC";
$get_posts_res = mysql_query($get_posts,$conn) or die(mysql_error());
// create the display string
$display_block = "<p>Showing posts for the <strong>$topic_title</strong> topic:</p><table width=100% cellpadding=3 cellspacing=1 border=1><tr><th>AUTHOR</th><th>POST</th></tr>";
while ($posts_info = mysql_fetch_array($get_posts_res)) {
$post_id = $posts_info['post_id'];
$post_text = nl2br(stripslashes($posts_info['post_text']));
$post_create_time = $posts_info['fmt_post_create_time'];
$post_owner = stripslashes($posts_info['post_owner']);
$posts_topics = "SELECT count(post_id) FROM forum_topics WHERE topic_owner = $posts_info[post_owner]";
$posts_topics_res = mysql_query($posts_topics,$conn) or die(mysql_error());
$posts_replies = "SELECT count(post_id) FROM forum_posts WHERE post_owner = $posts_info[post_owner]";
$posts_replies_res = mysql_query($posts_replies,$conn) or die(mysql_error());
$posts = $posts_topics_res + $posts_replies_res;
// add to display
$display_block .= "<tr><td width=35% valign=top>$post_owner<br>[$post_create_time]<br>Posts: $posts</td><td width=65% valign=top>$post_text<br><br><a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td></tr>";
}
// close up the table
$display_block .= "</table>";
}
?>
<html>
<head>
<title>Posts in Topic</title>
<link href="images/forum_style.css" type="text/css" rel="stylesheet">
</head>
<body>
<?php include 'header.php';?>
<h1>Posts In Topic</h1>
<?php print $display_block; ?>
<?php include 'footer.php';?>
</body>
</html>
In the while code block, I'm trying to gather the total amount of topics and replies a user has posted, then add them together. The error I get is: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Doe' at line 1", where Doe would be the last name of the topic owner John Doe. If the topic owner in the database for a certain post is an e-mail address, it will have @aol.com in the error message, where the topic owner is in johndoe@aol.com. Does anybody know what the problem is? Thanks.