虽然php上的循环不起作用但为什么?

时间:2016-12-28 23:10:51

标签: php

enter image description here我正在使用php进行项目CMS,我的 while循环之一无法正常工作,我无法找到原因。

我在循环内部(td)回显并且页面上没有显示任何内容但是当我在while循环之外回显时它工作得非常好。 我已经通过循环来看你。 在遇到问题的时候,你能给我一些帮助吗?

<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Author</th>
            <th>Comment</th>
            <th>Email</th>
            <th>Status</th>
            <th>In Response to</th>
            <th>Date</th>
            <th>Approve</th>
            <th>Unapprove</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>

<?php     
        $query = "SELECT * FROM comments ";
        $select_comments = mysqli_query($connection, $query);

        while($row = mysqli_fetch_assoc($select_comments)) {
            $comment_id = $row['comment_id'];
            $comment_post_id = $row['comment_post_id'];
            $comment_author = $row['comment_author'];
            $comment_content = $row['comment_content'];
            $comment_email = $row['comment_email']; 
            $comment_status = $row['comment_status']; 
            $comment_date = $row['comment_date']; 


            echo "<tr>";

            echo "<td>$comment_id</td>";
            echo "<td>$comment_author</td>";
            echo "<td>$comment_content</td>";
/
            echo "<td>$comment_email</td>";
            echo "<td>$comment_status</td>";

            //THIS IS THE LOOP DOESN'T WORK
            $query = "SELECT * FROM posts WHERE post_id = $comment_post_id";

            $select_post_id_query = mysqli_query($connection, $query);

            while($row = mysqli_fetch_assoc($select_post_id_query)) {
                $post_id = $row['post_id'];
                $post_title = $row['post_title'];

            echo "<td><a href='../post.php?p_id=$post_id'>$post_title</a></td>";
            }


            echo "<td>$comment_date</td>";
            echo "<td><a href='comment.php?approve=$comment_id'>Approve</a></td>";
            echo "<td><a href='comment.php?unapprove=$comment_id'>Unapprove</a></td>";
            echo "<td><a href='comment.php?delete=$comment_id'>Delete</a></td>";

            echo "</tr>";

        }
?>

    </tbody>      
</table>






<?php

//approve posts
if(isset($_GET['approve'])){

$the_comment_id = $_GET['approve'];

    $query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $the_comment_id ";
    $approve_comment_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}


//unapprove posts
if(isset($_GET['unapprove'])){

$the_comment_id = $_GET['unapprove'];

    $query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id =  $the_comment_id ";
    $unapprove_comment_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}

//delete posts
if(isset($_GET['delete'])){

$the_comment_id = $_GET['delete'];

    $query = "DELETE FROM comments WHERE comment_id = {$the_comment_id} ";
    $delete_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}



?>

2 个答案:

答案 0 :(得分:3)

您在第二个循环中覆盖变量$row。你应该改变它,例如。到$subRow以避免这种情况。

答案 1 :(得分:0)

我通过连接找到了我的数据库中缺少的部分。

THNX所有朋友:D