我目前正在开发一个评论系统,我想让用户评论一个现有的评论。
我为每条评论创建了一个链接,以便能够单击它以重定向到表单以编写对现有评论的答案。提交表单时,隐藏的输入应采用现有评论的当前 ID。但是有一个问题:每个评论的id在while循环中都是可用的,但是一旦循环完成,变量就会消失。所以我想知道如何获取每个评论的 id 以将其发送到隐藏的输入中。
这是我的完整代码:
<h2>Fiche</h2>
<?php
$_SESSION['cf']['message'] = $_GET['message'];
$reponse = db_query('SELECT * FROM messages WHERE id = ?', array($_SESSION['cf']['message']));
$donneesmsg = $reponse->fetch()
?>
<h5><?php echo $donneesmsg['Pseudo']; ?></h5>
<p class="text-center text-align"><?php echo $donneesmsg['Message']; ?></p>
<a href="index.php?page=fiche&message=<?php echo $_SESSION['cf']['message'] ?>&#formcom">Repondre</a>
<?php
echo '<h4>Commentaires</h4>';
echo '<hr>';
$reponse1 = db_query('SELECT * FROM comment WHERE messages_id = ?', array($donneesmsg['id']));
$donneescom1 = $reponse1->fetch();
$reponse = db_query('SELECT * FROM comment WHERE messages_id = ? ORDER BY id ASC', array($donneesmsg['id']));
while ($donneescom = $reponse->fetch()) {
if ($donneescom['commentaires_id'] == 0)
{
echo '<center>';
echo '' .$donneescom['id']. '<br>';
echo '' .$donneescom['pseudo']. '<br>';
echo '' .$donneescom['commentaire']. '<br>';
echo '<a href="index.php?page=fiche&message=' .$_SESSION['cf']['message']. '&com=' .$donneescom['id']. '#formcom">Repondre</a>';
echo '<hr>';
echo '</center>';
}
if ($donneescom['commentaires_id'] != 0)
{
echo 'En reponse au com ' .$donneescom['commentaires_id']. '<br>';
echo '' .$donneescom['id']. '<br>';
echo '' .$donneescom['pseudo']. '<br>';
echo '' .$donneescom['commentaire']. '<br>';
echo '<a href="index.php?page=fiche&message=' .$_SESSION['cf']['message']. '&com=' .$donneescom['id']. '#formcom">Repondre</a>';
echo '<hr>';
}
}
$reponse1 = db_query('SELECT * FROM comment WHERE messages_id = ?', array($donneesmsg['id']));
$donneescom1 = $reponse1->fetch();
?>
<form id="formcom" method="post" action="index.php?page=addcomment">
Pseudo: <input type="text" placeholder="Pseudo" name="pseudo"> <br>
Mail: <input type="email" placeholder="Mail" name="mail"> <br>
Commentaire: <textarea placeholder="Commentaire" name="commentaire"></textarea> <br>
<input type="hidden" name="date" value="<?php echo time(); ?>"><br>
<input type="hidden" name="messages_id" value="<?php echo $donneesmsg['id']; ?>"><br>
<input type="text" name="commentaires_id" value="<?php echo $donneescom1['id']; ?>"><br>
<input type="hidden" name="id" value="<?php echo $donneesmsg['id']; ?>"><br>
<input type="submit" value="Envoyer">
</form>