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