I admire you if you can fix this problem

My assignment in university is to create a forum using php. One of the subassignments is to be able to add a comment to a existing post.

I can create a comment to a existing post, but it displays my comment under the column username and my username in the column for comments

<?php
session_start();
require_once '/var/www/wits.ruc.dk/db.php';

// Retrieve post ID from URL parameter
if(isset($_GET['pid'])) {
    $pid = $_GET['pid'];
} else {
    // Redirect to main page if pid parameter is missing
    header("Location: index.php");
    exit();
}

// Retrieve post associated with the selected post ID
$post = get_post($pid);

// Retrieve all comments associated with the selected post ID
$cids_by_pid = get_cids_by_pid($pid);

// If the form has been submitted, add the comment to the database
if(isset($_POST['submit'])) {
    $content = $_POST['content'];
    $pid = $_POST['pid'];
    $uid = $_SESSION['uid'];
    add_comment($content, $pid, $uid);
    header("Location: comment.php?pid=$pid");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Kommentarer til <?php echo $post['title']; ?></title>
    <link rel="stylesheet" type="text/css" href="Stylesheet.css">
</head>
<body>
<h2>Kommentarer til <?php echo $post['title']; ?></h2>

<table class="outline-table">
    <thead>
    <tr>
        <th>Brugernavn</th>
        <th>Kommentar</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($cids_by_pid as $cid) {
        $comment = get_comment($cid);
        $user = get_user($comment['uid']);
        ?>
        <tr>
            <td><?php echo $user['uid']; ?></td>
            <td><?php echo $comment['content']; ?></td>
        </tr>
    <?php } ?>


    </tbody>
</table>

<form method="post">
    <label for="content">Tilføj kommentar:</label><br>
    <textarea id="content" name="content"></textarea><br>
    <input type="hidden" name="pid" value="<?php echo $pid; ?>">
    <input type="submit" name="submit" value="Tilføj kommentar">
</form>

<button onclick="window.location.href='posts.php'">Tilbage</button>

</body>
</html>

What is frustrating is, that it do show everybody els username in the username column and their comment in the comment column

To clarify: Maya is a username and “Hej du er ikke admin længere” is a comment but “Test” is a comment and “ccc” is a username.

How do i fix that?

Assuming that you have checked that the data is being stored correctly, what does the ‘view source’ of the output show in the browser?

I suspect that you either have some markup in with the values or some empty values, which is breaking the rendering of the table.

Sponsor our Newsletter | Privacy Policy | Terms of Service