PDO查询不从textarea插入信息

时间:2013-06-26 03:11:50

标签: php pdo

我正在尝试将帖子中的评论插入到我的数据库中。它不起作用,按下按钮提交按钮后页面刷新,但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;
    }
}

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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;
  }
}