2013-06-26 68 views
0

我想從帖子插入評論到我的數據庫。它不起作用,一旦按下按鈕提交按鈕,頁面就會刷新,但沒有信息從textarea上傳到數據庫。PDO查詢不插入信息從textarea

這是在PDO中使用bindParam語句的正確方法,可能是錯誤的?我可以使用相同的變量名稱,例如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; 
    } 
} 

回答

1

您重新分配$sth,因此永遠不會執行的INSERT,你需要第二SELECT之前添加$sth->execute();

... 
    $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; 
    } 
} 
+0

對不起,我還是有點困惑。我知道我重複使用$ sth 3次,所以不應該只是將$ sth更改爲另一個名稱,或者使用您的建議,我會刪除第一個$ sth-> execute(),並在整個查詢? – iBrazilian

+0

更新了我的答案。準備好'INSERT'和綁定參數後,需要使用'$ sth-> execute();'。執行語句之後,可以毫無問題地使用相同的變量。 –

+0

我明白了,這已經清理了很多東西,我感謝您的回答和時間! – iBrazilian

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; 
    } 
}