php - 使用 PHP 从 mysql 表中使用复选框删除多行

标签 php html mysql mysqli

我希望在通过复选框选择一行或多行时将其删除。但即使我选择它似乎也不会删除任何内容。

这是我的想法:

<?php
$con=mysqli_connect("localhost","root","","annualdb");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM asean_japan WHERE agency='Air'");

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

  for($i=0;$i<count((array)$checkbox);$i++) {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM asean_japan WHERE id='$del_id'";
    $result = mysqli_query($con,$sql);
  }
// if successful, refresh same page 
if($result) {
  echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
<table class='page'>
  <tr>
    <th>Select</th>
    <th>Agency</th>
    <th>FileName</th>
    <th>FileType</th>
    <th>Date Received</th>
    <th>Action</th>
  </tr>
<?php
while($row = mysqli_fetch_array($result))
{
  ?>
  <tr>
    <td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' value='<?php echo $row['id']; ?>'></td>
    <td><?php echo $row['agency']; ?></td>
    <td><?php echo $row['filename']; ?></td>
    <td><?php echo $row['filetype']; ?></td>
    <td><?php echo $row['date']; ?></td>
    <td><a target='_blank' href='../annual/indicators/" <?php echo $row['filename']; ?>"'>OPEN</a></td>
  </tr>
</table>

<?php
}
?>
<tr>
  <td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>
<?php
mysqli_close($con);
?>

我的复选框或 input=delete 可能有问题吗?

最佳答案

首先,您需要在 while 循环之外关闭表标记,然后您需要检查 onclick 您的数据发布是否指向目标 url click here this will help you

关于php - 使用 PHP 从 mysql 表中使用复选框删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486333/

相关文章:

php - 仅将新记录从一台数据库服务器插入到另一台数据库服务器

php - Codeigniter user_agent 不工作

html - 具有半透明背景而不影响其包含元素

javascript - EaselJS透视图像变换

javascript - OpenWeatherMap - map 仅显示莫斯科

sql - SELECT 子句中不存在聚合函数时的 GROUP BY 行为

php - Ajax PHP 创建的表单未提交

php - 选择带有单词 mysql 的 url 的 preg_match_all id

mysql - 我可以在 sql 查询中两次使用 where 子句吗?

php - MYSQL查询错误