When I post comments using the form field from the loop, the specific ID of the blog is not carried over to the php script. So the comment is not assigned to the blog where it is meant to go.
<?php
$conn = mysql_connect("localhost", "ooze", "");
mysql_select_db ("ooze");
$result = mysql_query ("select * from blog") or die(mysql_error());
$result2 = mysql_query ("select * from blog, blogcomment where blog.ID = blogcomment.blogID") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • <a href='#' onclick=\"toggle_visibility('something$i'); return false\">Comments</a><div id='something$i' style='display: none;'>";
$i++;
while($row = mysql_fetch_array($result2))
{
echo "<p class='third' >$row[commentdate] • $row[username]</p><p>said:</p> <p>$row[comment]</p>";
}
if ( isset ($_SESSION["gatekeeper"])) {
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>';
}
else {
echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>';
}
echo "</div>";
}
mysql_close($conn);
?>
dont use the same variable (
$row) for both loops…change your second loop to look as:
and change all the values from the second result to
$row2["COLUMN_NAME"]also – don’t forget to surround the array index with quotation marks… it works but creates a notice, and makes the server work a little bit harder
i.e:
$row[username]should be$row["username"]this is a try to make a little bit order in your code:
after all if i understood right what you were trying to achieve – than your script is not good, the second query should be inside the first loop and contain a
WHERE blogID={$row["ID"]}in the endif you prefer getting all the comments in one query – it is good, but you need to arrange them and then show only the relevant for each “blog”, when you fetch them in a nested loop – the second query will only give you rows ones (because in the second time – it will already be empty)