我当前的代码似乎删除了表格中的每一行。
只需要删除未选中的内容。
这是我的代码:
$assigned_array = array($_POST['employee_assigned']);
$assigned_employees = implode (",", $assigned_array);
// update unchecked assignments
// get all employees and delete the ones not in the checked list
$get_emp_sql = "SELECT * FROM `accounts` WHERE `permissions` > 0";
$get_emp_result = mysqli_query($dblink, $get_emp_sql);
while ($get_emp_row = mysqli_fetch_array($get_emp_result)) {
// delete rows that are not checked
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."' AND '".$get_emp_row['id']."' NOT IN ('".$assigned_employees."')";
if (mysqli_query($dblink, $delete_assignment)) {
} else {
die ("Unassignment error.");
}
}
这会删除每一行,包括已检查的框(然后添加,然后由问题删除)。
需要添加已选中,如果存在则删除任何未选中的内容。
答案 0 :(得分:1)
创建一个包含所有已检查员工ID的数组。
if (strlen($_POST['employee_assigned']) > 0) {
$assigned_employees = implode (",", $_POST['employee_assigned']);
}
// update unchecked assignments
// get all employees and delete the ones not in the clicked list
$get_emp_sql = "SELECT * FROM `accounts` WHERE `permissions` > 0";
$get_emp_result = mysqli_query($dblink, $get_emp_sql);
while ($get_emp_row = mysqli_fetch_array($get_emp_result)) {
if (strlen($assigned_employees == 0)) {
// delete all rows for this provider
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."'";
} else {
// delete rows that are not checked
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."' AND '".$get_emp_row['id']."' NOT IN ('".$assigned_employees."')";
}
if (mysqli_query($dblink, $delete_assignment)) {
} else {
die ("Unassignment error.");
}
}
// update checked assignments
if (strlen($_POST['employee_assigned']) > 0) {
foreach ($_POST['employee_assigned'] as $assign_this_employee) {
// check for existing assignment
$check_exist = "SELECT * FROM `provider_assignments` WHERE `provider_id` = '".$pro_id."' AND `employee_id` = '".$assign_this_employee."'";
$exist_result = mysqli_query($dblink, $check_exist);
$exist_count = mysqli_num_rows ($exist_result);
// if assignment doesn't exist, insert assignment
if ($exist_count == 0) {
// insert sql
$add_assignment_sql = "INSERT INTO `provider_assignments` (`provider_id`, `employee_id`) VALUES ('".$pro_id."', '".$assign_this_employee."')";
// execute sql
if (mysqli_query($dblink, $add_assignment_sql)) {
} else {
die ("assignment error");
}
}
}
}
此代码效果很好..欢迎任何评论!