【发布时间】:2017-09-28 06:44:19
【问题描述】:
我不明白为什么 mysqli_fetch_assoc 只显示一个结果而不是五个。我仔细检查了数据库,确定有 5 个条目..
如果我删除第二个 mysqli_fetch_assoc 则显示所有结果。
请注意:连接包括在上面..
任何意见和建议将不胜感激...谢谢!
<table class="table table-bordered table-hover">
thead>
<tr>
<th>Id</th>
<th>Author</th>
<th>Title</th>
<th>Category</th>
<th>Status</th>
<th>Image</th>
<th>Tags</th>
<th>Comments</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['delete'])){
$post_id = $_GET['delete'];
$query = "DELETE FROM posts WHERE post_id='$post_id'";
$delete_post_query = mysqli_query($con, $query);
}
$query = mysqli_query($con, "SELECT * FROM posts");
while($row = mysqli_fetch_assoc($query)) {
$post_id = $row['post_id'];
$post_category_id = $row['post_category_id'];
$post_title = $row['post_title'];
$post_author = $row['post_author'];
$post_status = $row['post_status'];
$post_image = $row['post_image'];
$post_content = $row['post_content'];
$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 = mysqli_query($con, "SELECT * FROM categories WHERE cat_id='$post_category_id'");
while($row = mysqli_fetch_assoc($query)) {
$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' alt='image' width='100'></td>";
echo "<td>$post_tags</td>";
echo "<td>$post_comment_count</td>";
echo "<td>$post_date</td>";
echo "<td><a href='posts.php?delete=$post_id'>Delete</a></td>";
echo "<td><a href='posts.php?source=edit_post&p_id=$post_id'>Edit</a></td>";
echo "</tr>";
}
?>
</tr>
</tbody>
</table>
【问题讨论】:
-
危险:你很容易受到SQL injection attacks的影响,你需要defend你自己。
-
我知道......!我将在最后解决所有安全问题..
-
这就像盖房子,然后在最后解决地基问题。你最终会撞倒墙壁来解决你一开始就建立的问题。然后你只需要毫无意义地重建墙壁。一开始就做好。
-
@MattDomer 这一定是 StackOverflow 上常见的最糟糕的态度之一
-
您编辑了问题以将 RESOLVED 放在顶部。这不是 Stackoverflow 的工作方式。请阅读How does accepting an answer work?