Почему мой код не отображает комментарии и ответы должным образом? - PullRequest
1 голос
/ 18 мая 2019

Это тестовый код, я в курсе SQL-инъекций и как их исправить. Это довольно длинный вопрос, поэтому я постараюсь изо всех сил.

Это столбцы в моей базе данных

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)

Я работаю над системой сообщений, которая позволит пользователям публиковать контент, а затем комментировать сообщения или отвечать на комментарии к сообщениям. У меня есть база данных под названием posts, которая содержит все комментарии, комментарии и ответы. Когда в базе данных нет комментария, имеющего то же значение postid, что и сообщение, ничего не должно отображаться Если в базе данных нет ответа с тем же postid и commentid, что и в предыдущем комментарии, ничего не должно повториться.

Другими словами, если нет комментария или ответа, ничего не должно появиться. У меня есть фактическое сообщение, которое нужно показать, но в моей базе данных есть комментарии и ответы, которые не отображаются, какие-либо идеи?

Мой код

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 class="PostPage">
                                <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 class="PostComments">
                                        <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 class="PostReplies">
                                                <p>
                                                    '.$replyrow['reply'].'
                                                </p>
                                            </div>
                                    ';
                                }
                            }
                        }
                    }
                }
            }
        }   
    }

Если у вас есть другие вопросы, я буду более чем рад ответить.

1 Ответ

0 голосов
/ 18 мая 2019

Похоже, эта часть ... AND postid = 'postid' AND ... должна быть ... AND postid = '$postid' AND ...

Ваш запрос ищет записи с postid == (строка) "postid" not postid == (значение) $ postid по вашему желанию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...