php – displaying comments under specific articles-ThrowExceptions

Exception or error:

So I have a searchbar(That searches articles by title,text,date and author) built in PHP and for each article(I have 2 articles) I want display its comments(ex. at article 1 I want to display comments that were written at article 1 and for that I did a foreign key for the comments table where I have the id of the article the comment was written. The problem is that it shows all the comments for each article(at the comments for article 1 it shows the comments for the article 1 and 2). I dont know how to write the code for showing the comments to the right article. I think that the path to what comments should show at each article in the getComments function is wrong. I’m very new to PHP and MYSQL and pls sry for my english.

article.php

    <div class="article-container">
        <?php
            $title= mysqli_real_escape_string($conn, $_GET['title']);
            $date= mysqli_real_escape_string($conn, $_GET['date']);

            $sql = "SELECT * FROM articol WHERE a_titlu='$title' AND a_data='$date'";
            $result = mysqli_query($conn, $sql);
            $queryResults = mysqli_num_rows($result);

            if ($queryResults > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<div class='article-box'>
                        <h3>".$row['a_titlu']."</h3>
                        <p>".$row['a_text']."<p>
                        <p>".$row['a_data']."<p>
                        <p>".$row['a_autor']."<p>
                    </div>

                    <form method='POST' action='".setComments($conn)."'>
                        <input type='hidden' name='uid' value='Anonymous'>
                        <input type='hidden' name='articolid' value='".$row['a_id']."'>
                        <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
                        <textarea name='message'></textarea><br>
                        <button class='button' name='commentSubmit' type='submit'>Comment</button>
                    </form>
                    </div><br><br>";
                } 
                getComments($conn, $row['a_id']);
            }
        ?>

setComments function

function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
    $uid = $_POST['uid'];
    $date = $_POST['date'];
    $message = $_POST['message'];
    $articolid = $_POST['articolid'];


    $sql = "INSERT INTO comments (uid, date, message, articol_id)
             VALUES ('$uid', '$date', '$message', '$articolid')";
    $result = $conn->query($sql);
}

}

getComments function

function getComments($conn) {
$sql = "SELECT c.uid, c.date, c.message
        FROM comments AS c LEFT JOIN articol AS p ON c.articol_id = p.a_id
        WHERE c.articol_id = p.a_id
        ORDER BY c.date DESC;";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
    echo "<div class='comment-box'><p>";
        echo $row['uid']."<br>";
        echo $row['date']."<br>";
        echo nl2br($row['message']);
    echo "</p></div>";
}

}

articol tabel

comments tabel

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *