Mysql删除数组中的多行 [英] Mysql delete multiple row in array
本文介绍了Mysql删除数组中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$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 删除数组中的多行.用户勾选复选框并将其发送到数组中.
I have a mysql delete multiple row in array. user tick checkbox and send it in array.
我把它内爆成字符串并使用IN(),我不知道哪里出错了,它只删除了一行.
I implode it into string and use IN (), I don't know where is went wrong, it only delete one row.
post_id user_id
2 1
3 1
4 2
所以如果用户:1 发送 $favour_delete=array(2,3);
它应该删除第一行和第二行
So if user: 1 send $favour_delete=array(2,3);
it should delete first and second row
推荐答案
不幸的是,现在有一种方法可以在查询中参数化 in 子句,您最好的选择如下.
Unfortunately as of now there is now way to parameterize the in clause in a query your best bet here is as follows.
<?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);
这篇关于Mysql删除数组中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文