0

我正在尝试从两个通过 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>
4

1 回答 1

0

Consider the following example...

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

So, now let's delete both the recipe and the ingredient from the database, but just for fun let's keep the '6,6' relationship in the recipe_ingredient table...

 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!
 +-----------+---------------+
于 2013-11-11T23:32:23.247 回答