I am creating a social site and I am trying to add a comment under a post.
When I try to add it from the comment_frame.php(where all of the comment code is) the comment uploads properly. But when I try to add a comment from the index.php page to the comment_frame.php page, nothing goes into the database and I don't get any errors. My code is below.
comment_frame.php(the code that works):
$post_id = $_POST['post_id'] ?? 0;
<form class='comment_frame.php' id='single_form' name='postComment<?php echo $post_id; ?>'
method='POST'>
<textarea name='post_body' rows="3" placeholder='Write a comment...'></textarea>
<input type='submit' name='postComment<?php echo $post_id; ?>' value='Post'>
</form>
if(isset($_POST['postComment' . $post_id])) {
if (empty($_POST["post_body"])) {
// echo "Comment can't be empty";
echo "Comment can't be empty. <a href=".$_SERVER['HTTP_REFERER'].">Try Again</a>";
//die() also terminates the script with display the message.
exit();
}
$post_body = trim(strip_tags(filter_var($_POST['post_body'], FILTER_SANITIZE_STRING)));
$stmt = $con->prepare("INSERT INTO comments (post_body, posted_by, posted_to, post_id)
VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $post_body, $userLoggedIn, $posted_to, $post_id);
$stmt->execute();
if($posted_to != $userLoggedIn) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $posted_to, 'comment');
}
$get_commenters = $con->prepare('SELECT post_body, posted_by, posted_to, date_added
FROM comments WHERE post_id = ? ORDER BY date_added DESC');
$get_commenters->bind_param("i", $post_id);
$get_commenters->execute();
$get_commenters->bind_result($post_body, $posted_by, $posted_to, $date_added);
$get_commenters_result = $get_commenters->get_result();
$notified_users = array();
while ($row = $get_commenters_result->fetch_assoc()) {
if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to
&& $row['posted_by'] != $userLoggedIn && !in_array($row['posted_by'], $notified_users)) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $row['posted_by'], "comment_non_owner");
array_push($notified_users, $row['posted_by']);
}
}
}
The code that doesn't work:
index.php:
<div class='comment_div'>
<form target='frame' class='comment_frame.php?post_id=$post_id'
id='comment_form' name='postComment" . $post_id . "'
method='POST'>
<textarea name='post_body' placeholder='Write a comment...'></textarea>
<input type='submit' name='postComment" . $post_id . "'
value='". $post_id ."'>
</form>
</div>
comment_frame.php:
$post_id = $_POST['post_id'] ?? 0;
if(isset($_POST['postComment' . $post_id])) {
if (empty($_POST["post_body"])) {
// echo "Comment can't be empty";
echo "Comment can't be empty. <a href=".$_SERVER['HTTP_REFERER'].">Try Again</a>";
//die() also terminates the script with display the message.
exit();
}
$post_body = trim(strip_tags(filter_var($_POST['post_body'], FILTER_SANITIZE_STRING)));
$stmt = $con->prepare("INSERT INTO comments (post_body, posted_by, posted_to, post_id)
VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $post_body, $userLoggedIn, $posted_to, $post_id);
$stmt->execute();
if($posted_to != $userLoggedIn) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $posted_to, 'comment');
}
$get_commenters = $con->prepare('SELECT post_body, posted_by, posted_to, date_added
FROM comments WHERE post_id = ? ORDER BY date_added DESC');
$get_commenters->bind_param("i", $post_id);
$get_commenters->execute();
$get_commenters->bind_result($post_body, $posted_by, $posted_to, $date_added);
$get_commenters_result = $get_commenters->get_result();
$notified_users = array();
while ($row = $get_commenters_result->fetch_assoc()) {
if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to
&& $row['posted_by'] != $userLoggedIn && !in_array($row['posted_by'], $notified_users)) {
$notification = new Notification($con, $userLoggedIn);
$notification->insertNotification($post_id, $row['posted_by'], "comment_non_owner");
array_push($notified_users, $row['posted_by']);
}
}
}
actionis right. You are making this request asGET, Since passing?post_id=$post_idinput type hidden