代码
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
命令是不处理。
最佳答案
:rid
和 :cmt
之间缺少逗号。
$stmt = $dbh->prepare("INSERT INTO `post_comments` (`user_id`,`post_id`,`reply_id`,`comment`,`date`) VALUES (:uid, :pid, :rid, :cmt, :date)");
关于php - 使用简单的 PDO 查询未将发布值输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150336/