$favour_delete=$_GET['favour_delete'];
$favour_delete=implode(",",$favour_delete);
$delete=$db->prepare("DELETE FROM favour WHERE post_id IN (:favour_delete) && user_id=:user_id");
$delete->bindValue(':favour_delete', $favour_delete, PDO::PARAM_STR);
$delete->bindValue(':user_id', $user_id, PDO::PARAM_STR);
我有一个mysql删除数组中的多行。用户勾选复选框并将其发送到数组中。
我将其内爆成字符串并使用IN(),我不知道哪里出错,它只删除一行。
post_id user_id
2 1
3 1
4 2
因此,如果用户:1发送$favour_delete=array(2,3);
,则应删除第一行和第二行
答案 0 :(得分:0)
不幸的是,到目前为止,没有办法在查询中参数化in子句,这里你最好的选择如下。
<?php
$favour_delete=$_GET['favour_delete'];
$favour_delete=array_map('intval',$favour_delete);
$delete=$db->prepare("DELETE FROM favour WHERE post_id IN (".implode(",",$favor_delete).") && user_id=:user_id");
$delete->bindValue(':user_id', $user_id, PDO::PARAM_STR);