我想從帖子插入評論到我的數據庫。它不起作用,一旦按下按鈕提交按鈕,頁面就會刷新,但沒有信息從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;
}
}
對不起,我還是有點困惑。我知道我重複使用$ sth 3次,所以不應該只是將$ sth更改爲另一個名稱,或者使用您的建議,我會刪除第一個$ sth-> execute(),並在整個查詢? – iBrazilian
更新了我的答案。準備好'INSERT'和綁定參數後,需要使用'$ sth-> execute();'。執行語句之後,可以毫無問題地使用相同的變量。 –
我明白了,這已經清理了很多東西,我感謝您的回答和時間! – iBrazilian