代码
if(isset($_POST['post_comment_submit'])){
//Get all values
$uid = $_SESSION['user']['id'];
$pid = $_POST['pid'];
$rid = $_POST['rid'];
$cmt = $_POST['post_reply_content'];
$date = date('c');
//Check that comment is not empty
if(empty($cmt)){
header('location: ../post.php?id='.$pid.'&error=incomplete');
exit;
}
//Connect to database
include "../inc/connect.php";
//Prepare statement
$stmt = $dbh->prepare("INSERT INTO `post_comments` (`user_id`,`post_id`,`reply_id`,`comment`,`date`) VALUES (:uid, :pid, :rid :cmt, :date)");
//Bind values
$stmt->bindParam(":uid", $uid);
$stmt->bindParam(":pid", $pid);
$stmt->bindParam(":rid", $rid);
$stmt->bindParam(":cmt", $cmt);
$stmt->bindParam(":date", $date);
//Execute
if($stmt->execute()){
header('Location: ../post.php?id='.$pid);
} else {
print_r("Error");
echo "<br>";
print_r($stmt);
echo "<br>";
print_r($_POST);
echo "<br>";
}
}
输出
Error
PDOStatement Object ( [queryString] => INSERT INTO `post_comments` (`user_id`,`post_id`,`reply_id`,`comment`,`date`) VALUES (:uid, :pid, :rid :cmt, :date) )
Array ( [pid] => 8 [rid] => 1 [post_reply_content] => Test Comment Reply [post_comment_submit] => Add comment )
解释
这是处理表单数据的PHP
页面。该功能是通过输入用户的ID,帖子的ID,评论的ID,回复,评论内容和评论的日期/时间来处理对页面评论的回复。
问题
如您所见,PHP
文件正在接收所有相关的$_POST
数据,但由于某种原因,SQL INSERT
命令未处理。
答案 0 :(得分:3)
您错过了:rid
和:cmt
之间的逗号。
$stmt = $dbh->prepare("INSERT INTO `post_comments` (`user_id`,`post_id`,`reply_id`,`comment`,`date`) VALUES (:uid, :pid, :rid, :cmt, :date)");