0
我正在處理的網站允許用戶發佈多個評論到網站,每個評論都有一個唯一的id,它在數據庫中是auto_incremented的。然後我將這些ID設置爲評論對象列表。在我的html中,然後檢索這些ID併爲每個評論對象創建一組喜歡和不喜歡的按鈕(帶有foreach循環)。Php比較html按鈕名稱與數據庫中的名稱
我遇到的問題是,當用戶點擊一個像按鈕時,與它的id(動態名稱)名稱相同的按鈕的名稱應該與該ID在數據庫中。但點擊按鈕只會改變屏幕上顯示的評論之一。我目前使用會話來存儲數組,並將其值發送到DA類,但沒有取得任何成功。
我不知道如何正確發送和比較名稱與數據庫中的ID。
的HTML:
<?php foreach ($comments->getComments() as $comment) : ?>
<!--The like button:-->
<form class="userActions" action="../Controller/index.php" method="post">
<input type="submit" name="<?php echo "comment_#" . $comment->getID() . "_Likes"; ?>" class="btnLike btnLikeStyle like" value="Like">
<input type="hidden" name="action" value="comment_like">
</form>
<!--Show number of likes on the page:-->
<div>
<!--display the likes number from the database-->
<label class="lblLikes"><?php echo htmlspecialchars($comment->getLikes()); ?></label>
</div>
<!--Dislike button:-->
<form class="userActions" action="../Controller/index.php" method="post">
<input type="submit" name="<?php echo "comment_#" . $comment->getID() . "_Likes"; ?>" class="btnDislike btnDislikeStyle dislike" value="Dislike">
<input type="hidden" name="action" value="comment_dislike">
</form>
<?php endforeach; ?>
控制器:
case 'comment_like':
$commentID = $comment->getID();
$comments = comments::addLike($theCommentID);
include('../View/home.php');
break;
case 'comment_dislike':
$commentID = $comment->getID();
$comments = comments::addDislike($theCommentID);
include('../View/home.php');
break;
評論類
function addLike($commentID) {
CommentDA::addCommentLike($commentID);
}
function addDislike($commentID) {
CommentDA::addCommentDislike($commentID);
}
的DA類:
public static function addCommentLike($commentID) {
$db = self::getConnection();
$query = 'UPDATE comments
SET commentLikes = CommentLikes + 1
WHERE CommentID = :commentIDPlaceholder';
$statement = $db->prepare($query);
$statement->bindValue(':commentIDPlaceholder', $commentID);
try {
$statement->execute();
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
$statement->closeCursor();
}
public static function addCommentLike($commentID) {
$db = self::getConnection();
$query = 'UPDATE comments
SET commentLikes = CommentLikes - 1
WHERE CommentID = :commentIDPlaceholder';
$statement = $db->prepare($query);
$statement->bindValue(':commentIDPlaceholder', $commentID);
try {
$statement->execute();
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
$statement->closeCursor();
}