javascript 无法从数据库中删除多条记录[重复]

kiz8lqtg  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(100)

此问题已在此处有答案

How can I bind an array of strings with a mysqli prepared statement?(7个回答)
How to include a PHP variable inside a MySQL statement(5个答案)
16小时前关闭
我在删除多条记录时遇到MySQL数据库中主键属性的数据类型问题。我试图删除数据库表中的多条记录,其主键属性具有“varchar”数据类型。我没有得到任何错误,但记录不删除。
然后我用“autoincrement”将主键属性的数据类型更改为“int”,并尝试使用相同的代码,然后它可以正常工作。却不知道为什么有人能看出是什么导致了这种奇怪的行为吗?我希望删除多个主键属性的数据类型为“varchar”的记录。”
tblmproduct表:
| 姓名|类型|
| --------------|--------------|
| Product_code(Primary)|中文(简体)|
| 批号|中文(简体)|
| 产品名称|中文(简体)|
| 生产日期|日期|
| EXP_date|日期|
| 供应商_id|整数(10)|
联系我们

<span class="custom-checkbox">
<input type="checkbox" class="product_checkbox" data-product-id="<?php echo $row["Product_code"]; ?>">
<label for="checkbox2"></label>
</span>

脚本:

$(document).on("click", "#delete_multiple", function () {
    var product = [];
    $(".product_checkbox:checked").each(function () {
      product.push($(this).data("product-id"));
    });
    if (product.length <= 0) {
      alert("Please select records.");
    } else {
      swal({
        title: "Are you sure?",
        text: "Once deleted, you will not be able to recover this Record !",
        icon: "warning",
        buttons: true,
        dangerMode: true,
      }).then((willDelete) => {
        if (willDelete) {
          var selected_values = product.join(",");
         
          $.ajax({
            type: "POST",
            url: "save-mproduct.php",
            cache: false,
            data: {
              type: 4,
              id: selected_values,
            },
            success: function (response) {
              var ids = response.split(",");
              for (var i = 0; i < ids.length; i++) {
                location.reload();
                $("#" + ids[i]).remove();
              }
            },
          });
          
          swal("Record Deleted successfully !", {
            icon: "success",
          });
  
        } else {
          swal("Your imaginary file is safe!");
        }
      });
    }
  });

save-mproduct.php:

if(count($_POST)>0){
    if($_POST['type']==4){
        $id=$_POST['id'];
        $sql = "DELETE FROM  tblmproduct WHERE Product_code IN ($id)";
        if (mysqli_query($con, $sql)) {
            echo $id;
        } 
        else {
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
        mysqli_close($con);
    }
}
brccelvz

brccelvz1#

试试这个

$id = $_POST['id'];
$arr_id = explode(',',$id);
$arr_id = array_map(function($e) {
    return "'".$e."'";
}, $arr_id);
$id = implode(',', $arr_id);
vlf7wbxs

vlf7wbxs2#

试试这个:

$array_id_list = explode(',',$id);
$count_id = count($array_id_list);
$max_i = $count_id - 1;
for ($i = 0; $i < $count_id; $i++) {
  $each_id = $array_id_list[$i];
  $deleted_id_list .= $each_id;
  $sql = "DELETE FROM  tblmproduct WHERE Product_code='$each_id'";
  ${'query_insert_'.$i} = mysqli_query($con,$sql);
  if ($i == $max_i) {
    echo $deleted_id_list;
    break;
  }
}

这一定能成功!

相关问题