如何为在mysql中接收数据的表创建编辑和删除?
我希望用数据库显示数据库中的所有帖子,并能够从数据库中删除和编辑帖子。
这是我的代码:
I want to show all post from database with table, with the ability to delete and edit posts from the database. It's my code:
<div id="addPost">
<table border="2">
<tr>
<th>ID</th>
<th>Author Name</th>
<th>Author ID</th>
<th>Date</th>
<th>Title</th>
<th>Content</th>
</tr>
<tr>
<?php
// Check connection
if (!can_connectToDBwithConstant()) {
return FALSE;
}
// Create connection
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = mysqli_query($con, "SELECT ID,post_author_name,post_author_id,post_date,post_title,post_content FROM `wp_posts`");
//php loop to load all post
while ($row = mysqli_fetch_array($query)):
$id = $row['ID'];
$authorName = $row['post_author_name'];
$authorID = $row['post_author_id'];
$date = $row['post_date'];
$title = $row['post_title'];
$content = $row['post_content'];
?>
<td><?php echo $id; ?></td>
<td><?php echo $authorName; ?></td>
<td><?php echo $authorID; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $content; ?></td>
<td>Edit | Delete</td>
</tr>
<?php endwhile; mysqli_close($con); ?>
</table>
问题是:怎么做我们知道哪些是选中的帖子来编辑或删除它?我怎么能这样做?
The problem is: How do we know which ones are selected posts to edit it or delete it?How can I do it?
有很多解决这个问题的方法之一,其中之一是通过taggin编辑和删除标签或其他一些元素(td,tr或从你已经拥有的id字段中获取值)。这是一个非常简单的(不是最好的,但它可以非常清楚地知道这件事的交易是什么):
There are a lot of ways to solve this, one of them is by taggin the Edit and Delete labels or some other element ( td, tr or taking the value from the id field you already have). Here's a very simple (not the best but it can give a very clear idea of what the deal is with this things):
<td>
<button id="edit_<?php echo $id ?>">Edit</button>
<button id="delete_<?php echo $id ?>">Delete</button>
</td>
通过这个你可以使用Jquery o javascript从按钮中提取id值。
By this you can extrac the id value from button using Jquery o javascript.