使用简单的PDO查询将未发布的值输入到数据库中

时间:2013-08-09 15:28:27

标签: php mysql sql post

代码

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命令未处理。

1 个答案:

答案 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)");