通过ajax,您可以获得$POST的数组。
这是我的密码。
<?php
include 'connect.php';
$deletethesestring = '';
foreach ($_POST as $v)
{
$deletethesestring .= $v.',';
}
$deletethesestring = rtrim($deletethesestring ,',');
echo $deletethesestring;
$deleteprizesqry = $pdo->prepare('DELETE FROM tbname where tbname_id IN(:deletethesestring);');
$deleteprizesqry -> bindParam(':deletethesestring', $deletethesestring);
$deleteprizesqry->execute();
?>
如果我的数组是例如[9,7,3,4,8,5]
我的代码只删除9个。剩下的人还在。我希望它删除数组中id所属的每一行。我该怎么做?
最佳答案
请尝试以下代码:
<?php
include 'connect.php';
$deletethesestring = '';
foreach ($_POST as $v)
{
$deletethesestring .= $v.',';
}
$deleteIds = rtrim($deletethesestring ,',');
$deletethesestring = join(',', array_fill(0, count($_POST), '?'));
$delQry = "DELETE FROM tbname where tbname_id IN($deletethesestring)";
$deleteprizesqry = $pdo->prepare($delQry);
$deleteprizesqry->execute($_POST);
希望这对你有帮助!