我正在尝试从两个通过 id 关联的表中删除行。我正在使用复选框进行删除,对于选中的行删除所有内容。
下面的代码从两个表中删除,但是当我检查两个结果时,它将从表一中删除其中两个,但只从表二中删除一行。如何能够从两个表中删除多行?foreach
对第一个查询有效,但对第二个查询无效。
有人可以给我一个提示吗?
<?php
if (isset($_POST['delete'])) {
if (!empty($_POST['id'])) {
$id = $_POST['id'];
$select = $mydb->prepare("select * FROM search where username = ? and id = ? ");
echo $mydb->error;
foreach ($_POST['id'] as $id) {
$select->bind_param('ss', $username->username, $id);
$select->execute();
$data = $select->get_result();
while ($row = $data->fetch_assoc()) {
$pdata = $row['productID'];
if ($row['producttype'] == 'laptop') {
$smt = $mydb->prepare("DELETE from search where username = ? and id = ?");
echo $mydb->error;
foreach ($_POST['id'] as $id) {
$smt->bind_param('ss', $username->username, $id);
$smt->execute();
$smt->close();
$smtr = $mydb->prepare("DELETE from laptops where username = ? and id = ?");
echo $mydb->error;
foreach ($_POST['id'] as $id) { // foreach was supposed to work but it doesn't work here I don't know why. it works on the first query.
$smtr->bind_param('ss', $username->username, $pdata); //$pdata from 1st query == id from second query.
$smtr->execute();
}
$smtr->close();
}
}
}
}
}
?>
<form action="" method="post">
<input type="submit" name="delete" class="styled-button-8" value="Delete">
<?php
$search = $mydb->prepare("select * from search where username = ? order by id");
$search->bind_param('s', $username->username);
$search->execute();
$res = $search->get_result();
while ($row = $res->fetch_assoc()) {
?>
<input type="checkbox" name="id[]" class="check" value="<?php echo $row['id'] ?>">
<?php
echo $row['title'];
echo $row['description'];
}
?>
</form>