first time here and I hope you guys can help me further.
I have created a blogging system to use by mine employees via an admin login system. As well a login system and registration system to use by my customers/viewers. So now I created a comment section for the blog system where people can commenting. The problem is that on each blog the comments are the same. How can link the comments to each blogID?
<?php ob_start(); require('config.php'); session_start(); $id = $_SESSION['user']; // if session is not set this will redirect to login page if( !isset($_SESSION['user']) ) { $buttonlog = 'Login'; $smglog = 'Log nu snel in!'; }if(isset($_SESSION['user'])) { $smglog = ' Logout'; $buttonlog = ' Mijn account'; } // select loggedin users detail $sql = mysql_query("SELECT * FROM `userlogin` WHERE `id` = '$id'"); $userRow = mysql_fetch_array($sql); $sql1 = mysql_query("SELECT * FROM `comments`"); $userRow1 = mysql_fetch_array($sql1); ?> <?php require('includes/config.php'); $stmt = $db->prepare('SELECT postID, postTitle, postCont, postDate, imgBackground, imgNewspost FROM blog_posts WHERE postID = :postID'); $stmt->execute(array(':postID' => $_GET['id'])); $row = $stmt->fetch(); //if post does not exists redirect user. if($row['postID'] == ''){ header('Location: ./'); exit; } ?> <?php function setComments(){ if(isset($_POST['commentSubmit'])){ $id = $_POST['id']; $username = $_POST['username']; $date = $_POST['date']; $message = $_POST['message']; mysql_query("INSERT INTO `comments` (id, username, date, message) VALUES ('$id','$username', '$date', '$message')"); } } ?> <?php function deleteComments(){ if(isset($_POST['DeleteComment'])){ $id = $_POST['id']; mysql_query("DELETE FROM `comments` WHERE `comments`.`id` = '$id'"); } } ?> <?php if (isset($_SESSION['user'])){ echo "<form method='POST' action='".setComments()."'> <input type='hidden' name='id' value=''> <input type='hidden' name='username' value='".$_SESSION['user']."'> <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'> <textarea name='message'> </textarea> <br> <input style='' type='submit' name='commentSubmit' value='REAGEER'> </form>"; }else{ echo " Log in om te reageren"; } ?> </div> <div class="reacties"><br></div> <?php $sql = mysql_query("SELECT * FROM `comments`"); while ($row = mysql_fetch_assoc($sql)){ $id= $row['username']; $sql2 = mysql_query("SELECT * FROM `userlogin` WHERE `id` = '$id'"); if ($row2 = mysql_fetch_assoc($sql2)){ echo "<div class='commentsDiv'>"; echo "<img style='margin: 5px; left: -25px; width: 100px; height: 100px; border: 1px black solid;position: relative; display: inline;'src='".$row2['image']."'>"; echo '<p style="color: #b73a09;font-size: 16px; font-weight: bold; top: 120px; left: 0px; display: inline; position: absolute;"> '.$row2['uname'].'</p>'; echo '<p style="color: #44444; font-size: 10px; left: 5px; top: -10px; display: inline; position: absolute;"> '.$row['date'].'</p>'; echo '<p style="font-family: Poppins, sans-serif; color: #44444; text-align: justify; padding-left: 150px; top: -125px; position: relative;"> '.nl2br($row['message']); echo ""; if(isset($_SESSION['user'])){ if($_SESSION['user'] == $row2['id']){ echo "<form class='commentDelete' method='POST' action='".deleteComments()."'> <input type='hidden' name='id' value='".$row['id']."'> <button name='DeleteComment'>Delete</button> </form> <form class='commentEdit' method='POST' action='editComments.php'> <input type='hidden' name='id' value='".$row['id']."'> <input type='hidden' name='username' value='".$row['username']."'> <input type='hidden' name='date' value='".$row['date']."'> <input type='hidden' name='message' value='".$row['message']."'> <button>Edit</button> </form>"; }else { echo "<form class='commentEdit' method='POST' action='replycomment.php'> <input type='hidden' name='id' value='".$row['id']."'> <button type=''>Reply</button> </form>"; } } else{ echo "<p class='commentMessage'> Je moet inglogd zijn om te reageren! </p>"; } echo "</div>"; } } ?>
Advertisement
Answer
When you are entering users’ comments in the comments table of your database.Send the blog-post-id(unique for every blog post) along with rest of the form. you can accomplish that using a hidden input like this
<input type="hidden" name="blogpost_id" id="blog_entry" value="<?php echo $postId; ?>"/>
and when you are loading comments on a blog-posts page select comments from comments table and sort them using that blog-post-id. like this
$query = "SELECT * FROM blog_comments WHERE blogpost_id='{$postId}'; $result = mysqli_query($connection,$query);