PHP:无法删除多个选定数据
目前,我创建了一个系统,允许用户使用复选框删除多个数据 步骤如下: 1) 用户在dashboard.php上选择团队,时间在dashboard.php之前,时间在dashboard.php之后。用户单击“搜索”按钮以显示结果。显示结果使用AJAX。AJAX将重定向到range.php 2) 用户可以选择显示和删除的任何数据行 我的问题是,删除功能失败,虽然我做了正确的步骤和代码。以下是当前代码: range.phpPHP:无法删除多个选定数据,php,mysql,ajax,Php,Mysql,Ajax,目前,我创建了一个系统,允许用户使用复选框删除多个数据 步骤如下: 1) 用户在dashboard.php上选择团队,时间在dashboard.php之前,时间在dashboard.php之后。用户单击“搜索”按钮以显示结果。显示结果使用AJAX。AJAX将重定向到range.php 2) 用户可以选择显示和删除的任何数据行 我的问题是,删除功能失败,虽然我做了正确的步骤和代码。以下是当前代码: range.php <?php require_once "../../..
<?php
require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$email = $_SESSION['login_user'];
if(isset($_POST["From"], $_POST["to"], $_POST["team"]))
{
$result = '';
$query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id = '".$_POST['team']."' AND report_date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."' ORDER BY ot_report.report_date DESC";
$sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sql -> execute();
if(isset($_POST['save'])){
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql2 = "DELETE FROM ot_report WHERE report_id=:report_id";
$query2 = $conn->prepare($sql2);
$query2->execute(array(':report_id' => $del_id));
header("Location: dashboard.php");
}
}
if($sql->rowCount() > 0)
{
echo'
<form method="post" action="">
<div><a href="../pdf.php?from='.$_POST["From"].'&to='.$_POST["to"].' &team='.$_POST["team"].'" target="_blank"><u>PDF View</u></a></div><br>
<div class="row" style="height: 300px; overflow-y: scroll;">
<div class="col-lg-12 grid-margin stretch-card">
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "10%"><input type="checkbox" id="checkAl"> All</th>
<th width = "3%">id</th>
<th width = "15%">Date</th>
<th width = "25%">Supervisor</th>
<th width = "30%">Task Name</th>
<th width = "10%">Status</th>
<th width = "7%">Action</th>
</tr>
</thead>
<tbody>';
$i=0;
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
$datereport = $row['report_date'];
$datereport2 = strtotime($datereport);
$report_date = date('d M Y', $datereport2);
$status=$row['report_status'];
if($status=="Pending")
{
$color="color:blue";
}
else
{
$color="color:green";
}
if ($row['ot_start'] == '00:00:00'){
$ot_start = '-';
}else{}
if ($row['ot_end'] == '00:00:00'){
$ot_end = '-';
}else{}
echo'<tr>';
echo '<td><input type="checkbox" id="checkItem" name="check[]" value='.$row['report_id'].'></td>';
echo '<td>'.$row["report_id"].'</td>';
echo '<td>'.$report_date.'</td>';
echo '<td>'.$row["fullname"].'</td>';
echo '<td>'.$row["task_name"].'</td>';
echo '<td align="center" style='.$color.'><strong>'.$status.'</strong></td>';
echo '<td align="center">';
echo '<a class="btn-view btn-primary btn-sm" href="view_task/view_task.php?report_id='. $row["report_id"] .'" data-toggle="tooltip">View</a>';
echo '<a class="btn-view btn-danger btn-sm" href="delete.php?report_id='. $row["report_id"] .'" onClick=\'return confirm("Do you want to remove team?")\'>Delete</a></td>';
echo '</td>';
echo '</tr>';
$i++;
}
echo '<tr>';
echo '<td><p align="center"><button type="submit" class="btn-danger btn-sm" name="save">DELETE</button></p></td>';
echo '</tr>';
echo '</form>';
}
else
{
echo '
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "5%">id</th>
<th width = "12%">Date</th>
<th width = "29%">Supervisor</th>
<th width = "23%">Task Name</th>
<th width = "7%">From</th>
<th width = "7%">To</th>
<th width = "10%">Status</th>
<th width = "7%">Action</th>
</tr>
<tr>
<td colspan="8">No report found</td>
</tr>';
}
echo '</body></table></div></div>';
}
?>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
尝试将此更改为:
$query = "DELETE FROM ot_report WHERE report_id LIKE ?";
$sql2 = $conn->prepare($query);
$sql2->bind_param("i", $del_id);
$sql2->execute();
首先,我要修复您在那里创建的非常无效的HTML,因为到处都有嵌套错误。OP使用的是PDO而不是mysqli
$query = "DELETE FROM ot_report WHERE report_id LIKE ?";
$sql2 = $conn->prepare($query);
$sql2->bind_param("i", $del_id);
$sql2->execute();