php - Why isn't my code displaying comments and replies properly?
This is test code, I'm aware of sql injections and how to fix them. This is a pretty long question to explain so I'll try my best.
These are the columns within my database
contentid (auto increments with every post, comment, and reply)
hostid (gets the value of the person who uploaded the original post)
postid (gets the value of the post, auto increments with every post)
userid (gets the value of the person who commented or replied)
date (gets the current time)
title (gets the title of the post entered by the host)
description (gets the description of the post entered by the host)
fileid (describes the type of post ex. video, picture, or blog)
commentid (auto increments with every comment)
comment (the actual comment entered by a user)
replyid (auto increments with every reply)
reply (the actual reply entered by a user)
I'm working on a post system that will allow users to post content, and then comment on posts or reply to comments from the posts. I have a database called posts that contains all posts comments and replies. When there isn't a comment in the database that has the same postid value as the post, nothing should echo. When there isn't a reply in the database that has the same postid and commentid as the comment before it, nothing should echo.
In other words, if there isn't a comment or reply nothing should pop up. I got the actual post to show up, but there are comments and replies inside my database that aren't showing up, any ideas?
My code
function getPost($conn) {
$userName = $_GET["user"];
$postid = $_GET["post"];
$usersql = "SELECT * FROM users WHERE userName = '$userName'";
$userresult = mysqli_query($conn, $usersql);
while ($userrow = mysqli_fetch_assoc($userresult)) {
$hostid = $userrow['userid'];
$postsql = "SELECT * FROM posts WHERE hostid = '$hostid' AND postid = '$postid' AND commentid = 0";
$postresult = mysqli_query($conn, $postsql);
while ($postrow = mysqli_fetch_assoc($postresult)) {
if (mysqli_num_rows($postresult)==0) {
echo '
<p>Error. Post does not exist.</p>
';
}
else {
$title = $postrow['title'];
$description = $postrow['description'];
$filename = "posts/".$hostid."/".$postid.".*";
$fileinfo = glob($filename);
$fileext = explode(".", $fileinfo[0]);
$fileactualext = $fileext[1];
echo '
<div >
<p>
'.$postrow['title'].'<br>
<img src="posts/'.$hostid.'/'.$postid.'.'.$fileactualext.'"><br>
'.$postrow['date'].'<br>
'.$postrow['description'].'
</p>
</div>
';
// this part of the code I can't get to show up on the website
$commentsql = "SELECT * FROM posts WHERE hostid = '$hostid' AND postid = 'postid' AND commentid > 0 AND replyid = 0";
$commentresult = mysqli_query($conn, $commentsql);
while ($commentrow = mysqli_fetch_assoc($commentresult)) {
if (mysqli_num_rows($commentresult)==0) {
echo '
<p>There are no Comments.</p>
';
}
else {
echo '
<div >
<p>
'.$commentrow['comment'].'
</p>
</div>
';
$currentcommentid = $commentrow['commentid'];
$replysql = "SELECT * FROM posts WHERE hostid = '$hostid' AND postid = '$postid' AND commentid = '$currentcommentid' AND replyid > 0";
$replyresult = mysqli_query($conn, $repysql);
while ($replyrow = myslqi_fetch_assoc($replyresult)) {
if (mysqli_num_rows($replyresult)==0) {
echo '';
}
else {
echo '
<div >
<p>
'.$replyrow['reply'].'
</p>
</div>
';
}
}
}
}
}
}
}
}
If you have any other questions I will be more than happy to answer.
Answer
Solution:
It looks like this part
... AND postid = 'postid' AND ...
should be... AND postid = '$postid' AND ...
Your query is looking for records with postid == (the string) "postid" not postid == (the value of) $postid as you intend.