0

您好,我有一个问题,我看不到页面上的评论。我在页面上没有收到任何错误,所以我被困在这一刻有人可以帮助我吗?

这是php代码:

<div id="container">
  <?php include('includes/menu.php');?>
        <div id="post">
            <?php
                $row = $query->fetch_object();
                echo "<h2>".$row->title."</h1>";
                echo "<p>".$row->body."</p>";
            ?>
        </div>
        <hr />
        <div id="add-comments">
            <form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
                <div>
                    <label>Email Adres</label><input type="text" name="email" />
                </div>
                <div>
                    <label>Naam</label><input type="text" name="name" />
                </div>
                <div>
                    <label>Commentaar</label><textarea name="comment"></textarea>
                </div>
                <input type="hidden" name="post_id" value="<?php echo $id?>" />
                <input type="submit" name="submit" value="Toevoegen"/>
            </form>
            </div>
            <hr />
          <div id="comments">
            <?php
                $query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
                while($row = $query->fetch_object()):
            ?>
              <div>
                    <h5><?php echo $row->name?></h5>
                    <blockquote><?php echo $row->comment?></blockquote>
            <?php endwhile;?>
            </div>

    </div>
</div>

页面的其余部分是:

    <div id="container">
  <?php include('includes/menu.php');?>
        <div id="post">
            <?php
                $row = $query->fetch_object();
                echo "<h2>".$row->title."</h1>";
                echo "<p>".$row->body."</p>";
            ?>
        </div>
        <hr />
        <div id="add-comments">
            <form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
                <div>
                    <label>Email Adres</label><input type="text" name="email" />
                </div>
                <div>
                    <label>Naam</label><input type="text" name="name" />
                </div>
                <div>
                    <label>Commentaar</label><textarea name="comment"></textarea>
                </div>
                <input type="hidden" name="post_id" value="<?php echo $id?>" />
                <input type="submit" name="submit" value="Toevoegen"/>
            </form>
            </div>
            <hr />
          <div id="comments">
            <?php
                $query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
                while($row = $query->fetch_object()):
            ?>
              <div>
                    <h5><?php echo $row->name?></h5>
                    <blockquote><?php echo $row->comment?></blockquote>
            <?php endwhile;?>
            </div>

    </div>
</div>

希望有人看到问题。

4

2 回答 2

1

尝试像这样打印评论..

 <div id="comments">
            <?php
                $query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
                while($row = $query->fetch_object()){ //opening while block
            ?>
              <div>
                    <h5><?php echo $row->name;?></h5>
                    <blockquote><?php echo $row->comment;?></blockquote>
            <?php }// ending while block ?>
            </div>

    </div>
于 2013-07-26T13:00:57.577 回答
0
<div id="comments">
        <?PHP
            $query = $db->query("SELECT name, comment FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
            while($row = $query->fetch_object()) {
        ?>
          <div>
                <h5><?= $row->name ?></h5>
                <blockquote><?= $row->comment ?></blockquote>
          </div>
        <?PHP  } ?>
 </div>

如果这不起作用,我建议转储 SQL 查询并确保它正常工作。

于 2013-07-26T13:01:35.060 回答