PHP社交站点评论功能 index页提交数据无法写入数据库无报错排查
问题背景
我正在开发一个社交站点,需要实现帖子下方添加评论的功能。
当我在存放所有评论逻辑的comment_frame.php页面提交评论时,数据可以正常上传写入数据库;但我从index.php页面向comment_frame.php提交评论请求时,没有任何数据写入数据库,也未返回任何错误信息,相关代码如下。
可正常运行的comment_frame.php代码
$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']); } } }
故障场景相关代码
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']); } } }
故障原因
存在两个核心错误导致提交失效:
- 表单提交地址配置错误:
index.php里的表单把请求地址写到了class属性里,没有配置action属性,表单默认会提交到当前页面index.php而不是comment_frame.php,请求根本没有送到处理逻辑页。 post_id没有传递到后端:comment_frame.php里从$_POST拿post_id,但index.php的表单里没有任何字段传递post_id,导致$post_id始终为0,isset($_POST['postComment' . $post_id])判断直接不成立,后续逻辑全部不执行。
修复方案
- 修改
index.php的表单代码,补全action属性,同时新增隐藏字段传递post_id:
<div class='comment_div'> <!-- 新增action属性指定提交地址 --> <form target='frame' action='comment_frame.php' id='comment_form' name='postComment<?php echo $post_id; ?>' method='POST'> <!-- 新增隐藏字段传递post_id --> <input type="hidden" name="post_id" value="<?php echo $post_id; ?>"> <textarea name='post_body' placeholder='写一条评论...'></textarea> <input type='submit' name='postComment<?php echo $post_id; ?>' value='发布'> </form> </div>
- 可在
comment_frame.php顶部加入以下代码开启错误提示,方便后续调试:
error_reporting(E_ALL); ini_set('display_errors', 1);
内容的提问来源于stack exchange,提问作者sir toby
相关产品推荐
相关产品推荐

