我的页面左侧有一个菜单,其中列出了帖子的所有标题。
menu.inc.php
<?php $rArticles = selectArticles($conn); ?>
<ul class="menu">
<?php while($row = mysqli_fetch_array($rArticles)){ ?>
<li><a href="index.php?page=articleform"><?php echo $row['artTitre'];?></a></li>
<?php } ?>
</ul>
我想要发生的是,当我点击标题时,帖子将显示在此页面上:
<?php $rArticles = selectArticles($conn); ?>
<h2>Articles <span><a class="btn" href="index.php?page=articleform">Ajouter un article</a></span></h2>
<div class="article">
<h3>
<?php
$rows = mysqli_fetch_array($rArticles);
$rArticlesId = selectArticleById( $rows['artID'], $conn);
while($row = mysqli_fetch_array($rArticlesId)){
?>
<a href="index.php?page=articleform&article=<?php echo $rows['artID']?>"> <?php echo $rows['artTitre']; ?></a>
<span><a class="btn" href="index.php?page=articles&action=delete&item=<?php echo $rows['artID'];?>">supprimer</a></span>
</h3>
<p><em><?php echo $rows['artDate']; ?> - <?php echo $rows['artAuteur']; ?></em></hp>
<p><?php echo $rows['artContenu']; ?></p>
<fieldset>
<legend> Commentaires </legend>
<?php
$rComm = selectCommentairesByIdArticle( $rows['artID'], $conn);
while($row = mysqli_fetch_array($rComm)){
?>
<p>
<a href="index.php?page=commentaires&action=delete&item=<?php echo $row['commentID'];?>"><img src="images/cross.png" /></a>
<strong><?php echo $row['commentPseudo']; ?> </strong><?php echo $row['commentText']; ?>
</p>
<?php } ?>
<form action="index.php?page=commentaires&action=insert" method="post">
<table class="admin_form" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="30%"><label for="PseudoCommentaire">Pseudo</label></td>
<td><input id="PseudoCommentaire" type="text" name="PseudoCommentaire" value="<?php if(isset($_POST['PseudoCommentaire'])) ?> " /></td>
</tr>
<tr>
<td valign="top"><label for="TexteCommentaire">Commentaire</label></td>
<td><textarea id="TexteCommentaire" name="TexteCommentaire"><?php if(isset($_POST['TextCommentaire'])) ?></textarea></td>
</tr>
<tr>
<td>
<input type="hidden" name="IdArticle" value="<?php echo $rows['artID'] ?>" />
</td>
<td>
<input type="submit" value="Envoyer le commentaire" />
</td>
</tr>
</table>
</form>
</fieldset>
<?php } ?>
</div>
我设法“删除”和“添加”帖子,但根据帖子ID显示和编辑失败。我的数据库连接和查询工作正常。我认为问题是指向页面。你能不能请教我这个?
答案 0 :(得分:0)
<强> menu.inc.php 强>
<?php $rArticles = selectArticles($conn); ?>
<ul class="menu">
<?php while($row = mysqli_fetch_array($rArticles)){ ?>
<li>
<a href="index.php?page=articleform&post_id=<?php echo $row['YOUR-POST-ID'];?>">
<?php echo $row['artTitre'];?>
</a>
</li>
<?php } ?>
</ul>
在显示/编辑页面
$post_id = $_GET['post_id']
答案 1 :(得分:0)
这样做的简单方法是从
添加您的唯一参数$row = mysqli_fetch_array($rArticles)
到您的菜单生成。假设您的唯一参数名为“id”,是一个具有自动增量的主键,并且由查询返回,它将是:
<?php $rArticles = selectArticles($conn); ?>
<ul class="menu">
<?php while($row = mysqli_fetch_array($rArticles)){ ?>
<li><a href="index.php?page=articleform&article=<?php echo $row['id'];?>"><?php echo $row['artTitre'];?></a></li>
<?php } ?>
</ul>
然后在下一页上,您可以使用(简化形式)从数据库中请求您的数据:
$id = (int)$_GET['article'];
$sql = "SELECT data FROM table WHERE id=$id LIMIT 1"; // place your SQL here
$rArticle = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($rArticle);
答案 2 :(得分:0)
适用于menu.inc.php
编辑:<a href="index.php?page=home&article=<?php echo $rows['artID'];?>">
<?php
$rArticles = selectArticles($conn); ?>
<ul class="menu">
<?php while($rows = mysqli_fetch_array($rArticles)){ ?>
<li><a href="index.php?page=home&article=<?php echo $rows['artID'];?>"><?php echo $rows['artTitre'];?></a></li>
<?php } ?>
</ul>
然后我又制作了另一个用于编辑文章的表格:articleformupdate.php
<?php $id = (int)$_GET['article'];
$sql = 'SELECT * FROM articles WHERE artID = \''.$id.'\'';
$rArticle = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($rArticle);
?>
<h2>Articles</h2>
<form action="index.php?page=articles&action=update" method="post">
<table class="admin_form" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="30%"><label for="TitreArticle">Titre</label></td>
<td><input id="TitreArticle" type="text" name="TitreArticle" value="<?php
if(isset($_POST['TitreArticle'])){
echo ($_POST['TitreArticle']);
}else{
echo $row['artTitre'];
}
?>" /></td>
</tr>
<tr>
<td width="30%"><label for="AuteurArticle">Auteur</label></td>
<td><input id="AuteurArticle" type="text" name="AuteurArticle" value="<?php
if(isset($_POST['AuteurArticle'])){
echo ($_POST['AuteurArticle']);
}else{
echo $row['artAuteur'];
}
?>" /></td>
</tr>
<tr>
<td valign="top"><label for="ContenuArticle">Contenu de l'article</label></td>
<td><textarea id="ContenuArticle" name="ContenuArticle"><?php
if(isset($_POST['ContenuArticle'])){
echo ($_POST['ContenuArticle']);
}else{
echo $row['artContenu'];
} ?></textarea></td>
</tr>
<tr>
<td>
<input type="hidden" name="dateArticle" value="" />
<input type="hidden" name="IdArticle" value="<?php
if(isset($_POST['IdArticle'])){
echo ($_POST['IdArticle']);
}else{
echo $row['artID'];
} ?>" />
</td>
<td>
<a class="btn" href="index.php">Annuler</a>
<input type="submit" value="Envoyer" />
</td>
</tr>
</table>