1

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

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

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

前端

//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 部分删除服务器上的文件。谢谢

4

2 回答 2

4

在html页面中这样做

<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");
    }
}
于 2012-04-13T17:54:50.930 回答
2

** MYSQL 代码 ** 描述:此代码最初创建要使用的数据库。在 MySQL 终端中:

`CREATE database `tester`;`

现在,为该数据库创建一个表:

`USE tester;`

`CREATE TABLE `test_mysql` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(80) NOT NULL,
`lastname` VARCHAR(80) NOT NULL,
`e_mail` VARCHAR(100) NOT NULL)
ENGINE = MYISAM;`

现在,在新创建的表中插入一些记录:

`INSERT INTO `test_mysql`
(`id`, `name`, `lastname`, `e_mail`)
VALUES
(NULL, 'Billly', 'Blueton', 'bb5@phpeasystep.com'),
(NULL, 'Jame', 'Campbell', 'jame@somewhere.com'),
(NULL, 'Mark', 'Jackson', 'mark@phpeasystep.com'),
(NULL, 'Linda', 'Travor', 'lin65@phpeasystep.com'),
(NULL, 'Joey', 'Ford', 'fordloi@somewhere.com'),
(NULL, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');`

接下来,创建一个名为 index.php 的 PHP (HTML) 文件:

** PHP & HTML 代码 **

`<h1>Deleting Multiple Records using PHP &amp; MySQL</h1>
<p>&nbsp;</p>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="tester"; // Changed database name
$tbl_name="test_mysql";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</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>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td></tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>

<tr><td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
</td><td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?>    </td>
<td bgcolor="#FFFFFF"><?php echo $rows['e_mail']; ?>
</td></tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td></tr>
<?php

if(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
print $sql;
$result = mysql_query($sql);}

if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}}
mysql_close();
?>
</table></form></td></tr></table>
<p>Record count: <?php echo number_format($count) ?></p>`
于 2012-06-27T17:00:41.890 回答