I have a problem with a comment system. Adding comments works, and the MySql table shows the correct comments that have been submitted. However, when echoing the contents of the table the comment is printed for every registered user. i.e. instead of just saying… Hello by James on 11/04/2012 it will say Hello by James on 11/04/2012 Hello by Tom on 11/04/2012 Hello by Emma on 11/04/2012 etc.
Like I said, the MySql table only shows one comment and it shows the correct user_id number of the user who uploaded the comment (which corresponds to the id in the users table). It must be the way I am echoing the comments but I’m new to PHP and MySql and I’m not sure I’m joining the tables correctly.
I’d be grateful for any help.
require_once("db_connect.php");
if($db_server) {
//If a connection to the database is made...
mysql_select_db($db_database) or die ("<p>Couldn't find database</p>");
//Print out existing comments
$result = mysql_query("SELECT * FROM comments JOIN users ON comments.profile_id = $problemID");
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j<$rows; ++$j) {
$str_comments .= "<span class='comment'>" . mysql_result($result, $j, 'comment') . "</span>";
$str_comments .= " bt " . mysql_result($result, $j, 'username');
$str_comments .= " on " . mysql_result($result, $j, 'comm_date') . "</br><br/>";
}
//Get any submitted comments and insert into database
$comment = clean_string($_POST['comment']);
if ($comment != '') {
//If the submitted comment is not empty...
if (strlen($comment)<200) {
mysql_query("INSERT INTO comments (`user_id`, `profile_id`, `comment`)
VALUES ('{$_SESSION['user_id']}', $problemID, ('$comment'))") or die(mysql_error());
}else{
$message = "Comment not submitted. The comment must be less than 200 characters long";
}
}
?>
In your query I don’t see a WHERE clause. Should be something like this:
A join only joins to tables together, but it doesn’t “filter out” results, for that you still need the WHERE clause