14

我想从 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>

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

4

6 回答 6

15

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

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

只有这样,您才能对其进行计数并将其循环以进行删除。

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

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

你的不包括它:

$result = mysqli_query($sql);
于 2013-01-23T08:17:22.283 回答
13

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

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

那是一个单一的查询来删除它们。

注意:您需要对传入的值进行转义$_POST['checkbox']mysql_real_escape_string类似以防止SQL 注入

于 2013-01-23T08:20:07.710 回答
4
<?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);

        }
    }
} 
于 2013-10-05T11:53:01.947 回答
4

使用 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);
  }
}
于 2014-05-19T22:36:16.027 回答
1

有时你可能会/可能不会意识到的事情

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

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

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

于 2013-01-23T08:33:01.720 回答
1
 $deleted = $_POST['checkbox'];
 $sql = "DELETE FROM $tbl_name WHERE id IN (".implode(",", $deleted ) . ")";
于 2017-01-01T12:08:41.233 回答