对所有人来说,今天好,我遇到了一个问题,当我尝试回复某个评论时,所有评论都是重复的,同时重复的评论不会显示在数据库中,而只会显示在页面上。感觉就像当您发送评论时,不仅发送了一条评论,而且至少发送了其他评论,看来,对数据库的请求已正确发出,并且信息以相同的方式显示: enter image description here enter image description here enter image description here enter image description here
Php代码:
<?php foreach ($showcomn as $result) {?>
<?php foreach ($showreplycom as $resultreply) {?>
<?php
if (isset($_SESSION['all-good'])) {
echo '<p id="user-comment-info">'.$result['name'].' '.$result['date'].'<br></p>',
'<div id="comment-user">',
'<p id="stealth-'.$result['id'].'">'.$result['comment'].'</p>',
'</div>',
'<form method="post" action="./check%20info/editsavechanges.php">';
if ($findid['id'] === $result['userID']){
echo '<textarea id="id-edit-save-'.$result['id'] .'" name="id-each-user" hidden>'.$result['id'].'</textarea>',
'<textarea name="edit-comment" id="edit-user-comment-'.$result['id'].'" class="edit-style" hidden >'.$result['comment'].'</textarea>',
'<div id="buttons-edit">',
'<button type="submit" name="save-edit-comment" id="save-edit-user-comment-'.$result['id'].'" hidden>Save</button>',
'<button type="button" onclick="cancelEdit('.$result['id'].')" id="cancel-edit-user-comment-'.$result['id'] .'" hidden>Cancel</button>',
'</div>',
'<div id="buttons-forms">',
'<button type="button" onclick="showForm('.$result['id'].')" id="open-edit-'.$result['id'].'" >Edit</button>',
'<button type="submit" name="delete-edit-comment" id="delete-edit-'.$result['id'].'" >Delete</button>',
'</div>';
}
echo '<div id="buttons-forms-reply">',
'<button type="button" onclick="showformReply('.$result['id'].')" id="reply-edit-'.$result['id'].'" >Reply</button>',
'</div>',
'<textarea name="reply-userid" id="comment-userid-reply" class="edit-style-user-id" readonly hidden>'.$findid['id'].'</textarea>',
'<textarea name="reply-user-name" id="comment-name-reply" class="edit-style-user" readonly hidden>'. $_SESSION['all-good']['login'] .'</textarea>',
'<textarea id="reply-save-comment'.$result['id'] .'" name="reply-comment-id" hidden>'.$result['id'].'</textarea>',
'<input type="hidden" name="reply-user-date">',
'<textarea name="reply-comment" id="reply-text-'.$result['id'].'" class="edit-style-reply" hidden ></textarea>';
echo '<div id="reply-buttons">',
'<button type="submit" name="save-reply-comment" id="save-reply-edit-'.$result['id'] .'" hidden>Save</button>',
'<button type="button" onclick="cancelReply('.$result['id'].')" id="cancel-reply-edit-'.$result['id'] .'" hidden>Cancel</button>','<br>',
'</div>',
'<div id="show-comments-reply">',
'<button type="button" onclick="showallReply(' . $resultreply['id'] . ')" id="show-reply-all-' . $resultreply['id'] . '" class="all_reply_button">Show all comments</button>',
'<button type="button" onclick="closeallReply(' . $resultreply['id'] . ')" id="close-reply-all-' . $resultreply['id'] . '" class="close_reply_button" hidden>Close all comments</button>',
'</div>',
'<div id="new_box_reply">',
'<p id="all-reply-com-' . $resultreply['id'] . '" class="reply_box" hidden>' . $resultreply['name'] . ' ' . $resultreply['date'] . '<br>' . $resultreply['comment'] . '</p>',
'</div>',
'</form>';
此处实现答复表格
<?php include_once "../connection/coments_connection.php";
date_default_timezone_set('Europe/Kiev');
if (isset($_POST['save-reply-comment'])) { $commentID = $_POST['reply-comment-id'];
$name = $_POST['reply-user-name'];
$userID = $_POST['reply-userid'];
$comment = $_POST['reply-comment'];
$data = date('Y-m-d H:i:s');
$replyBase = "INSERT INTO `replycom`(`commentID`, `name`, `comment`, `date`, `userID`) VALUES ('$commentID', '$name', '$comment', '$data', '$userID')";
$resultReply = $Gcom->prepare($replyBase);
$resultReply->execute([
'reply-comment-id' => $commentID,
'user-name' => $name,
'reply-comment' => $comment,
'reply-user-date' => $data,
'reply-userid' => $userID,
]);
header('Location: ../index.php#contacts'); }
以下是主要评论的Foreach循环:
<?php
include_once "./connection/coments_connection.php";
$showcomn = $Gcom->query("SELECT * FROM `comments` ORDER BY date DESC ");
$showcomn = $showcomn->fetchAll();
这是一个Foreach循环,用于回复评论:
<?php
include_once "./connection/coments_connection.php";
$showreplycom = $Gcom->query("SELECT * FROM `replycom` ORDER BY date DESC ");
$showreplycom = $showreplycom->fetchAll();