I am working on a commenting system for my website I am having some issues. As of right now user can comment and "reply" but the issue is coming in with the replies. I can't get the post_id to associate with the reply table of my comment.
There is a table called `posts` has
post_id-id of the post
id-id of the user who made the post
created_on-time stamp of post creation
content-text of the post or images, videos etc.
There is a table called `replies` has
reply_id-id of the reply
id-id of user who made the reply
created_on-time stamp of reply
content-text of the reply
post_id-this is the post that the user is replying to this is where we are running into issues.
Here is the code that I am working on. PHP using MySQLi prepared statements.
this is the comment.php file
<?php
require_once "config/config.php";
include "functions.php";
db_connect();
check_auth();
$sql = "INSERT INTO replies (id, post_id, content) VALUES(?,?,?)";
$statement = $conn->prepare($sql);
$statement->bind_param('isi', $_SESSION['id'], $_POST['content'], $_GET['post_id']);
if($statement->execute())
{
redirect_to("dashboard.php");
}else{
redirect_to("dashboard.php?error=true");
}
this is the section where the user can trigger the reply code
$result = get_posts($conn);
if($result->num_rows > 0 )
{
while($post = $result->fetch_assoc())
{
$sql = "SELECT id, first_name, last_name, pro_img FROM accounts WHERE id = ? LIMIT 1";
$statement = $conn->prepare($sql);
$statement->bind_param('i', $post['id']);
$statement->execute();
$statement->store_result();
$statement->bind_result($id, $first_name, $last_name, $pro_img);
$statement->fetch();
?>
<!--user posts-->
<div class="user_posts">
<label><?php echo '<img src="data:pro_img/jpg;base64,'.base64_encode($pro_img).'" height="50" width="50"/>';?><?php echo ($row['first_name']), ($row['last_name']);?></label>
<p><?php echo $post['content'];?></p>
<div class="likes">
<button class="like"><i class="material-icons like">favorite</i></button>
<button class="like"><i class="material-icons dislike">heart_broken</i></button>
</div>
<form action="comment.php?post_id=<?php $_GET['post_id'];?>" method="post">
<input type="text" name="content" id="content" required>
<input type="submit" name="comment" value="comment">
</form>
</div>
As you can see I am attempting to use $_GET to get the post_id to associate the reply with the comment but the post_id somehow always being associated with the "id" of the user instead of the id of the post we are trying to reply to.
here is the code to get the posts to show on the screen.
function get_posts($conn)
{
$sql = "SELECT * FROM posts ORDER BY posted_on DESC";;
$statement = $conn->prepare($sql);
$statement->execute();
return $statement->get_result();
}
Any kind of help would be much appreciated thank you in advanced.