页面上的评论不会出现

时间:2013-07-26 12:55:13

标签: php

您好我有问题我无法看到页面上的评论。 我没有在页面上得到任何错误,所以我被困在这一刻 有人能帮帮我吗?

这是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>

希望有人看到问题。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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查询并确保它正常工作。