通过使用 PHP 选择复选框来删除多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14475096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:21:43  来源:igfitidea点击:

Delete multiple rows by selecting checkboxes using PHP

phphtmlmysqlcheckboxisset

提问by spyder

I want to delete multiple rows from MYSQL database. I have created this delete.php file to select various links and delete them using checkboxes.

我想从 MYSQL 数据库中删除多行。我创建了这个 delete.php 文件来选择各种链接并使用复选框删除它们。

<html>
<head>

<title>Links Page</title>

</head>

<body>

<h2>Choose and delete selected links.</h2>

<?php

$dbc = mysqli_connect('localhost','root','admin','sample')
or die('Error connecting to MySQL server');

$query = "select * from links ORDER BY link_id";

$result = mysqli_query($dbc,$query)
or die('Error querying database');

$count=mysqli_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="3" bgcolor="#FFFFFF"><strong>Delete multiple links</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link URL</strong></td>
</tr>

<?php

while ($row=mysqli_fetch_array($result)) {
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox" type="checkbox" value="<?php echo $row['link_id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_url']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>
<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="3" bgcolor="#FFFFFF"><strong>Delete multiple links</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link URL</strong></td>
 </tr>

<?php

while ($row=mysqli_fetch_array($result)) {
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox" type="checkbox" value="<?php echo $row['link_id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['link_url']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>



<?php

// Check if delete button active, start this 

if(isset($_POST['delete']))
{
    $checkbox = $_POST['checkbox'];

for($i=0;$i<count($checkbox);$i++){

$del_id = $checkbox[$i];
$sql = "DELETE FROM links WHERE link_id='$del_id'";
$result = mysqli_query($sql);
}
// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=view_links.php\">";
}
 }

mysqli_close($dbc);

?>

</table>
</form>
</td>
</tr>
</table>

</body>

</html>

This doesn't seem to delete any row. My data is populated in the table. I guess the problem is with the PHP code. Please help me out here.

这似乎没有删除任何行。我的数据填充在表中。我想问题出在 PHP 代码上。请帮帮我。

采纳答案by Edwin Alex

You should treat it as an array like this,

你应该把它当作一个这样的数组,

<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">

Then only, you can take its count and loop it for deletion.

只有这样,您才能获取其计数并将其循环删除。

You also need to pass the database connection to the query.

您还需要将数据库连接传递给查询。

$result = mysqli_query($dbc, $sql);

Yours did not include it:

你的不包括它:

$result = mysqli_query($sql);

回答by Shiplu Mokaddim

Use array notation like name="checkbox[]"in your inputelement. This will give you $_POST['checkbox']as array. In the query you can utilize it as

name="checkbox[]"在您的input元素中使用数组表示法。这将为您$_POST['checkbox']提供数组。在查询中,您可以将其用作

$sql = "DELETE FROM links WHERE link_id in ";
$sql.= "('".implode("','",array_values($_POST['checkbox']))."')";

Thats one single query to delete them all.

那是将它们全部删除的单个查询。

Note: You need to escape the values passed in $_POST['checkbox']with mysql_real_escape_stringor similar to prevent SQL Injection.

注意:您需要逃避传递的值$_POST['checkbox']mysql_real_escape_string或类似的,以防止SQL注入

回答by user1900387

<?php $sql = "SELECT * FROM guest_book";
                            $res = mysql_query($sql);
                            if (mysql_num_rows($res)) {
                            $query = mysql_query("SELECT * FROM guest_book ORDER BY id");
                            $i=1;
                            while($row = mysql_fetch_assoc($query)){
                            ?>


<input type="checkbox" name="checkboxstatus[<?php echo $i; ?>]" value="<?php echo $row['id']; ?>"  />

<?php $i++; }} ?>


<input type="submit" value="Delete" name="Delete" />

if($_REQUEST['Delete'] != '')
{
    if(!empty($_REQUEST['checkboxstatus'])) {
        $checked_values = $_REQUEST['checkboxstatus'];
        foreach($checked_values as $val) {
            $sqldel = "DELETE from guest_book WHERE id = '$val'";
           mysql_query($sqldel);

        }
    }
} 

回答by user3654452

Delete Multiple checkbox using PHP Code

使用 PHP 代码删除多个复选框

<input type="checkbox" name="chkbox[]  value=".$row[0]."/>
<input type="submit" name="delete" value="delete"/>
<?php
if(isset($_POST['delete']))
{
 $cnt=array();
 $cnt=count($_POST['chkbox']);
 for($i=0;$i<$cnt;$i++)
  {
     $del_id=$_POST['chkbox'][$i];
     $query="delete from $tablename where Id=".$del_id;
     mysql_query($query);
  }
}

回答by Dave

Something that sometimes crops up you may/maynot be aware of

有时会突然出现的东西,您可能/可能不知道

Won't always be picked up by by $_POST['delete'] when using IE. Firefox and chrome should work fine though. I use a seperate isntead which solves the problem for IE

使用 IE 时不会总是被 $_POST['delete'] 选中。不过 Firefox 和 chrome 应该可以正常工作。我使用单独的 istead 解决了 IE 的问题

As for your not deleting in your code above you appear to be echoing out 2x sets of check boxes both pulling the same data? Is this just a copy + paste mistake or is this actually how your code is?

至于您没有在上面的代码中删除,您似乎在回显 2x 组复选框,两者都提取相同的数据?这只是一个复制+粘贴错误还是这实际上是你的代码?

If its how your code is that'll be the problem as the user could be ticking one checkbox array item but the other one will be unchecked so the php code for delete is getting confused. Either rename the 2nd check box or delete that block of html surely you don't need to display the same list twice ?

如果您的代码是这样的,那将是问题,因为用户可能会勾选一个复选框数组项,但另一个将未选中,因此用于删除的 php 代码变得混乱。重命名第二个复选框或删除该 html 块,您肯定不需要两次显示相同的列表?

回答by Oladimeji Ajeniya

 $deleted = $_POST['checkbox'];
 $sql = "DELETE FROM $tbl_name WHERE id IN (".implode(",", $deleted ) . ")";