Php 从多个表中删除
我正在尝试从两个表中删除由id关联的行。我正在使用复选框进行删除,对于选中的行,请全部删除 下面的代码从两个表中删除,但当我检查两个结果时,它将从表1中删除其中两个,但只从表2中删除一行。如何从两个表中删除多行Php 从多个表中删除,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试从两个表中删除由id关联的行。我正在使用复选框进行删除,对于选中的行,请全部删除 下面的代码从两个表中删除,但当我检查两个结果时,它将从表1中删除其中两个,但只从表2中删除一行。如何从两个表中删除多行foreach对第一个查询有效,但对第二个查询无效 有人能给我一个提示吗 <?php if (isset($_POST['delete'])) { if (!empty($_POST['id'])) { $id = $_POST['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>
考虑以下示例
SELECT * FROM recipes;
+-----------+-------------------------+
| recipe_id | recipe |
+-----------+-------------------------+
| 1 | Macaroni & Cheese |
| 2 | Cheese on Toast |
| 3 | Beans on Toast |
| 4 | Cheese & Beans on Toast |
| 5 | Toast & Jam |
| 6 | Bananas |
+-----------+-------------------------+
SELECT * FROM ingredients;
+---------------+------------+
| ingredient_id | ingredient |
+---------------+------------+
| 1 | Macaroni |
| 2 | Cheese |
| 3 | Beans |
| 4 | Toast |
| 5 | Jam |
| 6 | Bananas |
+---------------+------------+
SELECT * FROM recipe_ingredient;
+-----------+---------------+
| recipe_id | ingredient_id |
+-----------+---------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 4 |
| 3 | 3 |
| 3 | 4 |
| 4 | 2 |
| 4 | 3 |
| 4 | 4 |
| 5 | 4 |
| 5 | 5 |
| 6 | 6 | <-- The popular recipe 'Bananas' is made from bananas
+-----------+---------------+
SELECT r.*, i.*
FROM recipes r
JOIN recipe_ingredient ri
ON ri.recipe_id = r.recipe_id
JOIN ingredients i
ON i.ingredient_id = ri.ingredient_id;
+-----------+-------------------------+---------------+------------+
| recipe_id | recipe | ingredient_id | ingredient |
+-----------+-------------------------+---------------+------------+
| 1 | Macaroni & Cheese | 1 | Macaroni |
| 1 | Macaroni & Cheese | 2 | Cheese |
| 2 | Cheese on Toast | 2 | Cheese |
| 2 | Cheese on Toast | 4 | Toast |
| 3 | Beans on Toast | 3 | Beans |
| 3 | Beans on Toast | 4 | Toast |
| 4 | Cheese & Beans on Toast | 2 | Cheese |
| 4 | Cheese & Beans on Toast | 3 | Beans |
| 4 | Cheese & Beans on Toast | 4 | Toast |
| 5 | Toast & Jam | 4 | Toast |
| 5 | Toast & Jam | 5 | Jam |
| 6 | Bananas | 6 | Bananas | <-- See?
+-----------+-------------------------+---------------+------------+
从配方中选择*;
+-----------+-------------------------+
|配方_id |配方|
+-----------+-------------------------+
|1 |通心粉和奶酪|
|2 |烤面包上的奶酪|
|3 |吐司上的豆子|
|4 |烤面包上的奶酪和豆类|
|5 |烤面包和果酱|
|6 |香蕉|
+-----------+-------------------------+
从配料中选择*;
+---------------+------------+
|成分_id |成分|
+---------------+------------+
|1 |通心粉|
|2 |奶酪|
|3 |豆|
|4 |敬酒|
|5 |堵塞|
|6 |香蕉|
+---------------+------------+
从配方和配料中选择*;
+-----------+---------------+
|配方|配料| id|
+-----------+---------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 4 |
| 3 | 3 |
| 3 | 4 |
| 4 | 2 |
| 4 | 3 |
| 4 | 4 |
| 5 | 4 |
| 5 | 5 |
|6 | 6 |你缺少/错放了一些}开始with@TomToms这就是为什么只有一行被删除的原因吗?好吧,我不太理解这个例子。那么,你是说不能从第二个查询中删除吗?我是说你只需要一个查询。谢谢,但是对我来说,处理两个查询更容易,我不需要在
上获得连接
,。你对我的问题有什么建议吗?
DELETE r
, i
FROM recipes r
JOIN recipe_ingredient ri
ON ri.recipe_id = r.recipe_id
JOIN ingredients i
ON i.ingredient_id = ri.ingredient_id
WHERE recipe = 'Bananas';
Query OK, 2 rows affected (0.01 sec)
SELECT * FROM recipes;
+-----------+-------------------------+
| recipe_id | recipe |
+-----------+-------------------------+
| 1 | Macaroni & Cheese |
| 2 | Cheese on Toast |
| 3 | Beans on Toast |
| 4 | Cheese & Beans on Toast |
| 5 | Toast & Jam |
+-----------+-------------------------+
SELECT * FROM ingredients;
+---------------+------------+
| ingredient_id | ingredient |
+---------------+------------+
| 1 | Macaroni |
| 2 | Cheese |
| 3 | Beans |
| 4 | Toast |
| 5 | Jam |
+---------------+------------+
SELECT * FROM recipe_ingredient;
+-----------+---------------+
| recipe_id | ingredient_id |
+-----------+---------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 4 |
| 3 | 3 |
| 3 | 4 |
| 4 | 2 |
| 4 | 3 |
| 4 | 4 |
| 5 | 4 |
| 5 | 5 |
| 6 | 6 | <-- Still here!
+-----------+---------------+