从多个表中删除

时间:2013-11-11 23:19:55

标签: php mysql mysqli

我正在尝试从两个与id相关的表中删除行。我正在使用复选框进行删除,对于选中的行将全部删除。

以下代码从两个表中删除但是当我检查两个结果时,它将从表1中删除其中两个,但只从表2中删除一行。如何从两个表中删除多行? 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>

1 个答案:

答案 0 :(得分:0)

考虑以下示例...

 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?
 +-----------+-------------------------+---------------+------------+

所以,现在让我们从数据库中删除配方和成分,但为了好玩,让我们在recipe_ingredient表中保持'6,6'关系......

 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!
 +-----------+---------------+