使用复选框,PHP和MySQL删除多行 [英] Deleting multiple rows using checkboxes, PHP and MySQL

查看:90
本文介绍了使用复选框,PHP和MySQL删除多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题显示,我想从数据库中删除多行。为了完成这个,我有两个文件,一个前端文件,它生成一个表格,显示一个用户可以删除的文件,这些文件是使用复选框选择的。



后端文件是处理所选复选框,并使用SQL语句删除所选文件。



我遇到的问题是将所选文件的ID从前端传递到背部。这两个文件的代码如下:



前端


  //构建表查询
$ query =SELECT * FROM documents;
$ result = mysqli_query($ con,$ query)or die(Invalid query);

$ count = mysqli_affected_rows($ con);

?>
< table width =400border =0cellspacing =1cellpadding =0>
< tr>
< td>< form name =form1method =postaction =deletefilesback.php>
< table width =800border =0cellpadding =3cellspacing =2bgcolor =#CCCCCC>
< tr>
< td colspan =5bgcolor =#FFFFFFalign =center>< strong>删除多个文件< / strong>< / td>
< / tr>
< tr>
< td align =centerbgcolor =#FFFFFF>#< / td>
< td align =centerbgcolor =#FFFFFF>< strong> Id< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong>标题< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong>说明< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong>文件位置< / strong>< / td>
< / tr>
<?php
while($ row = mysqli_fetch_array($ result)){
?>
< tr>
< td align =centerbgcolor =#FFFFFF>< input name =checkbox []type =checkboxid =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 =5align =centerbgcolor =#FFFFFF>< input name =deletetype =submitid =deletevalue =Delete Files> ;< / TD>
< / tr>
< / table>
< / form>
< / td>
< / tr>
< / table>

后端


  $ delete = $ _POST ['checkbox']; 

//然后使用所选项目执行所需操作://
foreach($ delete as $ id){

$ query =DELETE FROM documents WHERE id ='。$ id。';
$ result = mysqli_query($ con,$ query)or die(Invalid query);

}
//显示项目已成功删除//
if(mysqli_affected_rows($ con)> 0){
echo' p>所选项目已成功删除。< / p>';
} else {
echo'< p>处理您的请求时发生错误< / p>';
}
?>

注意,一旦这样工作,我将使用unlink函数删除文件服务器使用前端表格的doc_link部分。
感谢

解决方案

在html页面上这样做

 < input name =checkbox [<?php echo $ row ['id']?>]

,后端如此这样

  foreach($删除为$ id => $ val){
if($ val =='checked'){
$ query =DELETE FROM documents WHERE id ='$ id。
$ result = mysqli_query($ con,$ query)or die(Invalid query);
}
}


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.

The back end file is to process the selected checkboxes and use an SQL statement to delete the chosen files.

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:

Front End

//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>

Back End

$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>';
}
?>

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

解决方案

in html page do it like this

<input name="checkbox[<?php echo $row['id']?>]"

and in the back end do like this

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删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆