我试图通过单击复选框和按钮从SQL数据库中删除表行。我的PHP代码:
<?php
$conn = mysqli_connect("localhost","root","","cms");
$view_posts = "SELECT * from editorial ";
$post_array = $conn->query($view_posts) or die($conn->error);
if(isset($_POST['delete'])){
$del = $_POST["all_id"];
foreach ($del as $x) {
$delete_qur = "DELETE from editorial where id='$x'";
$conn->query($delete_qur);
}
}?>
在该(相同文件)下还有更多代码可用于表格显示数据:
<body>
<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post">
<button class="button" name="delete">Delete</button>
<button class="button" name="edit">Edit</button>
</form>
<table border="1">
<th>PostID</th>
<th>Title</th>
<th>Post</th>
<th>Created</th>
<th>Options</th>
<?php while($row = $post_array->fetch_assoc()) : ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["title"]; ?></td>
<td><?php echo $row["post"]; ?></td>
<td><?php echo $row["created"]; ?></td>
<td><form action="<?php htmlspecialchars($SERVER['PHP_SELF']);?>" method="post">
<input type="checkbox" name="all_id[]" value="<?php echo $row['id']; ?>">
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
我想要做的是让每一行都有自己的复选框。在单击复选框后,我可以删除或编辑该特定行。对不起,不好的编码习惯,PHP中的新功能。