仅显示一条添加的记录,无法在CMS管理面板中查看更多记录


Shows only one added record, cant view more in CMS admin panel

我正在做PHP课程,需要构建CMS系统。问题:在管理面板中仅显示一行添加的帖子信息:示例。如果我添加更多帖子,它必须显示更多这样的行。帮帮我,我找不到错误?

源代码:

view_all_posts.php

<?php
    $query = "SELECT * FROM posts";
    $select_posts = mysqli_query($connection, $query);
    /* check there are records in recordset */
    if( mysqli_num_rows( $select_posts ) > 0 ) {
        while ($row = mysqli_fetch_assoc($select_posts)) {
            $post_id = $row['post_id'];
            $post_author = $row['post_author'];
            $post_title = $row['post_title'];
            $post_category_id = $row['post_category_id'];
            $post_status = $row['post_status'];
            $post_image = $row['post_image'];
            $post_tags = $row['post_tags'];
            $post_comment_count = $row['post_comment_count'];
            $post_date = $row['post_date'];
        }
        echo "<tr>";
        echo "<td>$post_id</td>";
        echo "<td>$post_author</td>";
        echo "<td>$post_title</td>";
        $query = "SELECT * FROM categories WHERE cat_id = {$post_category_id} ";
        $select_categories_id = mysqli_query($connection, $query);
        while ($row = mysqli_fetch_assoc($select_categories_id)) {
            $cat_id = $row['cat_id'];
            $cat_title = $row['cat_title'];
            echo "<td>{$cat_title}</td>";
        }
        echo "<td>$post_status</td>";
        echo "<td><img src='../images/$post_image' width='150' height='50'></td>";
        echo "<td>$post_tags</td>";
        echo "<td>$post_comment_count</td>";
        echo "<td>$post_date</td>";
        echo "<td><a href='posts.php?source=edit_post&p_id={$post_id}'>Edit</a></td>";
        echo "<td><a href='posts.php?delete={$post_id}'>Delete</a></td>";
        echo "</tr>";
    }
    ?>

add_post.php

<?php
if(isset($_POST['create_post'])){
    $post_title = $_POST['title'];
    $post_author = $_POST['author'];
    $post_category_id = $_POST['post_category'];
    $post_status = $_POST['post_status'];
    $post_image = $_FILES['image']['name'];
    $post_image_temp = $_FILES['image']['tmp_name'];
    $post_tags = $_POST['post_tags'];
    $post_content = $_POST['post_content'];
    $post_date = date('y-m-d');

    move_uploaded_file($post_image_temp, "../images/$post_image" );
    $query = "INSERT INTO posts (post_category_id, post_title, post_author, post_date,
 post_image, post_content, post_tags, post_status) ";
    $query .= "VALUES({$post_category_id}, '{$post_title}', '{$post_author}', now(),
    '{$post_image}', '{$post_content}', '{$post_tags}', '{$post_status}' ) ";
    $create_post_query = mysqli_query($connection, $query);
    confirmQuery($create_post_query);
}
?>

<form action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="post_title">Post Title
            <input type="text" class="form-control" name="title">
        </label>
    </div>
    <div class="form-group">
        <label for=""></label>
        <select name="post_category" id="">
            <?php
            $query = "SELECT * FROM categories";
            $select_categories = mysqli_query($connection, $query);
            confirmQuery($select_categories);
            while($row = mysqli_fetch_assoc($select_categories )) {
                $cat_id = $row['cat_id'];
                $cat_title = $row['cat_title'];
                echo "<option value='$cat_id'>{$cat_title}</option>";
            }
            ?>
        </select>
    </div>
    <div class="form-group">
        <label for="title">Post Author
            <input type="text" class="form-control" name="author">
        </label>
    </div>
    <div class="form-group">
        <label for="post_status">Post Status
            <input type="text" class="form-control" name="post_status">
        </label>
    </div>
    <div class="form-group">
        <label for="post_image">Post Image
            <input type="file" name="image">
        </label>
    </div>
    <div class="form-group">
        <label for="post_tags">Post Tags
            <input type="text" class="form-control" name="post_tags">
        </label>
    </div>
    <div class="form-group">
        <label for="post_content">Post Content
            <textarea class="form-control" name="post_content" id="" cols="30" rows="10"></textarea>
        </label>
    </div>

    <div class="form-group">
        <input class="btn btn-primary" type="submit" name="create_post" value="Publish Post">
    </div>
</form>

似乎这只是一个小错误,您必须在$select_postswhile循环中移动categoriesSELECT查询。

因为在给定的代码中,只有最后一条记录将被传递到下一个循环while以获取类别并最终打印到浏览器。

因此,正确的代码应如下所示。

    { 
    while ($row = mysqli_fetch_assoc($select_posts)) {
         $post_id = $row['post_id']; 
         $post_author = $row['post_author']; 
         $post_title = $row['post_title']; 
         $post_category_id = $row['post_category_id']; 
         $post_status = $row['post_status']; 
         $post_image = $row['post_image']; 
         $post_tags = $row['post_tags']; 
         $post_comment_count = $row['post_comment_count']; 
         $post_date = $row['post_date']; 
         echo ""; 
         echo "$post_id"; 
         echo "$post_author"; 
         echo "$post_title"; 
         $query = "SELECT * FROM categories WHERE cat_id = {$post_category_id} "; 
         $select_categories_id = mysqli_query($connection, $query); 
         while ($row = mysqli_fetch_assoc($select_categories_id)) { 
            $cat_id = $row['cat_id']; 
            $cat_title = $row['cat_title']; 
            echo "{$cat_title}"; 
         } 
         echo "$post_status"; 
         echo ""; 
         echo "$post_tags"; 
         echo "$post_comment_count"; 
         echo "$post_date"; 
         echo "Edit"; 
         echo "Delete"; 
         echo ""; 
    }
} 

像这样使用它:

在循环中打印数据

<?php
    $query = "SELECT * FROM posts";
    $select_posts = mysqli_query($connection, $query);
    /* check there are records in recordset */
    if( mysqli_num_rows( $select_posts ) > 0 ) {
        while ($row = mysqli_fetch_array($select_posts)) {
            $post_id = $row['post_id'];
            $post_author = $row['post_author'];
            $post_title = $row['post_title'];
            $post_category_id = $row['post_category_id'];
            $post_status = $row['post_status'];
            $post_image = $row['post_image'];
            $post_tags = $row['post_tags'];
            $post_comment_count = $row['post_comment_count'];
            $post_date = $row['post_date'];
            echo "<tr>";
        echo "<td>$post_id</td>";
        echo "<td>$post_author</td>";
        echo "<td>$post_title</td>";
        $query = "SELECT * FROM categories WHERE cat_id = {$post_category_id} ";
        $select_categories_id = mysqli_query($connection, $query);
        while ($row = mysqli_fetch_array($select_categories_id)) {
            $cat_id = $row['cat_id'];
            $cat_title = $row['cat_title'];
            echo "<td>{$cat_title}</td>";
        }  // End of Category Loop
        echo "<td>$post_status</td>";
        echo "<td><img src='../images/$post_image' width='150' height='50'></td>";
        echo "<td>$post_tags</td>";
        echo "<td>$post_comment_count</td>";
        echo "<td>$post_date</td>";
        echo "<td><a href='posts.php?source=edit_post&p_id={$post_id}'>Edit</a></td>";
        echo "<td><a href='posts.php?delete={$post_id}'>Delete</a></td>";
        echo "</tr>";
      }   // End of Posts Loop
    }  // End of IF
    ?>