0

我正在尝试将帖子中的评论插入到我的数据库中。它不起作用,一旦按下按钮提交按钮,页面就会刷新,但没有来自 textarea 的信息上传到数据库。

这是将 bindParam 语句与 PDO 一起使用的正确方法吗?可能有什么问题?我可以使用相同的变量名称,例如 uID 和 postiD,就像您在 3 个查询 SELECT 和 INSERT 中定义的那样。

PUBLIC FUNCTION Insert_Comment( $uiD, $post_iD, $comment ){
    $ip = $_SERVER['REMOTE_ADDR'];
    $sth = $this->db->prepare("SELECT com_id,comment FROM comments WHERE uid_fk = :uiD AND msg_id_fk = :post_iD ORDER by com_id DESC limit 1 ");
    $sth->bindParam(":uiD",         $uiD);
    $sth->bindParam(":postiD",      $post_iD);
    $sth->execute();

    $result = $sth->fetchAll();

    if ($comment!=$result['comment']){
        $sth = $this->db->prepare("INSERT INTO comments (comment, uid_fk,msg_id_fk,ip,created) VALUES ( :comment, :uiD, :postiD, :ip, :time)");
        $sth->bindParam(":comment",     $comment);
        $sth->bindParam(":uiD",         $uiD);
        $sth->bindParam(":postiD",      $post_iD);
        $sth->bindParam(":ip",          $ip);
        $sth->bindParam(":time",        time());

        $sth = $this->db->prepare("SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username 
                                FROM comments C, users U 
                                WHERE C.uid_fk = U.uiD 
                                AND C.uid_fk = :uiD 
                                AND C.msg_id_fk = :postiD 
                                ORDER by C.com_id 
                                DESC limit 1");
        $sth->bindParam(":uiD",         $uiD);
        $sth->bindParam(":postiD",      $post_iD);
        $sth->execute();
        $result = $sth->fetchAll();
        return $result;
     } else {
    return false;
    }
}
4

2 回答 2

1

您正在重新分配$sth,因此INSERT永远不会执行,您需要$sth->execute();在第二个之前添加SELECT

    ...
    $sth->bindParam(":ip",          $ip);
    $sth->bindParam(":time",        time());

    $sth->execute();

    $sth = $this->db->prepare(...)

详细:

PUBLIC FUNCTION Insert_Comment( $uiD, $post_iD, $comment ){
    $ip = $_SERVER['REMOTE_ADDR'];
    $sth = $this->db->prepare("SELECT com_id,comment FROM comments WHERE uid_fk = :uiD AND msg_id_fk = :post_iD ORDER by com_id DESC limit 1 ");
    $sth->bindParam(":uiD",         $uiD);
    $sth->bindParam(":postiD",      $post_iD);
    $sth->execute();

    $result = $sth->fetchAll();

    if ($comment!=$result['comment']){
        $sth = $this->db->prepare("INSERT INTO comments (comment, uid_fk,msg_id_fk,ip,created) VALUES ( :comment, :uiD, :postiD, :ip, :time)");
        $sth->bindParam(":comment",     $comment);
        $sth->bindParam(":uiD",         $uiD);
        $sth->bindParam(":postiD",      $post_iD);
        $sth->bindParam(":ip",          $ip);
        $sth->bindParam(":time",        time());

        /**
         * Insertion will happen just after executing the statement
         */
        $sth->execute();

        $sth = $this->db->prepare("SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username 
                                FROM comments C, users U 
                                WHERE C.uid_fk = U.uiD 
                                AND C.uid_fk = :uiD 
                                AND C.msg_id_fk = :postiD 
                                ORDER by C.com_id 
                                DESC limit 1");
        $sth->bindParam(":uiD",         $uiD);
        $sth->bindParam(":postiD",      $post_iD);
        $sth->execute();
        $result = $sth->fetchAll();
        return $result;
     } else {
    return false;
    }
}
于 2013-06-26T03:15:22.153 回答
1

我会像这样重写你的代码......将所有 bindParams 放在一个数组中,然后执行$sth......

public function Insert_Comment( $uiD, $post_iD, $comment ){
$ip = $_SERVER['REMOTE_ADDR'];
$sth = $this->db->prepare("SELECT com_id,comment FROM comments WHERE uid_fk = :uiD AND msg_id_fk = :post_iD ORDER by com_id DESC limit 1 ");
$sth->execute(array(":uiD"=>$uiD,":postID"=>$postiD));

$result = $sth->fetchAll();

if ($comment!=$result['comment']){
    $sth = $this->db->prepare("INSERT INTO comments (comment, uid_fk,msg_id_fk,ip,created) VALUES ( :comment, :uiD, :postiD, :ip, :time)");
    $sth->execute(array(":comment"=>$comment,":uiD"=>$uiD,":postID"=>$postiD,":ip"=>$ip,":time"=>time() ));


    $sth = $this->db->prepare("SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username 
                            FROM comments C, users U 
                            WHERE C.uid_fk = U.uiD 
                            AND C.uid_fk = :uiD 
                            AND C.msg_id_fk = :postiD 
                            ORDER by C.com_id 
                            DESC limit 1");
    $sth->execute(array(":uiD"=>$uiD,":postID"=>$postiD));
    $result = $sth->fetchAll();
    return $result;
 } else {
  return false;
  }
}
于 2013-06-26T03:20:23.013 回答