0

投稿のコメントをデータベースに挿入しようとしています。ボタン送信ボタンが押されるとページが更新されますが、テキストエリアからの情報はデータベースにアップロードされません。

これは PDO で bindParam ステートメントを使用する正しい方法ですか?何が問題なのですか? SELECT と INSERT の 3 つのクエリで定義されているように、uID や postiD などの同じ変数名を使用できますか。

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();。2 番目の の前に追加する必要があり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 に答える