問題描述
正如標題所示,我想從我的數據庫中刪除多行.為了實現這一點,我有兩個文件,一個前端文件,它生成一個表格,顯示用戶可以刪除的文件,這些文件使用復選框選擇.
As the title suggests I want to delete multiple rows from my database. To accomplish this I have two files, a front end file that generates a table that shows the files a user may delete which are chosen using checkboxes.
后端文件是對選中的復選框進行處理,并使用SQL語句刪除選中的文件.
The back end file is to process the selected checkboxes and use an SQL statement to delete the chosen files.
我遇到的問題是將選定文件的 id 從前端傳遞到后端.兩個文件的代碼如下:
The problem I am having is passing the id of a selected file from the front end to the back. The code for both files are below:
前端
//Build Table Query
$query="SELECT * FROM documents";
$result= mysqli_query($con, $query) or die("Invalid query");
$count = mysqli_affected_rows($con);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="deletefilesback.php">
<table width="800" border="0" cellpadding="3" cellspacing="2" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF" align="center"><strong>Delete Multiple Files</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>File Location</strong></td>
</tr>
<?php
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $row['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['title']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['description']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['doc_link']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete Files"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
后端
$delete = $_POST['checkbox'];
//Then do what you want with the selected items://
foreach ($delete as $id) {
$query="DELETE FROM documents WHERE id = '".$id."'";
$result= mysqli_query($con, $query) or die("Invalid query");
}
//Show that the items have been successfully removed.//
if (mysqli_affected_rows($con) > 0) {
echo '<p>The selected items have been successfully deleted.</p>';
} else {
echo '<p>An error has occurred while processing your request</p>';
}
?>
請注意,一旦這個工作正常,我將使用 unlink 函數使用前端表的 doc_link 部分刪除服務器上的文件.謝謝
As a note, once this is working I will be using the unlink function to delete the file on the server using the doc_link part of the table on the front end. Thanks
推薦答案
在html頁面中這樣做
in html page do it like this
<input name="checkbox[<?php echo $row['id']?>]"
在后端這樣做
foreach ($delete as $id => $val) {
if($val=='checked'){
$query="DELETE FROM documents WHERE id = '".$id."'";
$result= mysqli_query($con, $query) or die("Invalid query");
}
}
這篇關于使用復選框、PHP 和 MySQL 刪除多行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!