无法删除表中具有另一个表中相应相关值的条目

时间:2019-03-31 16:58:54

标签: php mysql sql mysqli

作为餐馆管理网站的一部分,我正在尝试更新和删除一些商品。 restaurant表具有主键rest_id,并使用此键链接到餐厅items表。

当我尝试删除未填充items中相应项目的餐厅中的条目时,我的代码有效。但是,当餐厅中的条目在items表中有一些记录时,删除查询将不执行任何操作,而只是刷新页面。

我正在尝试删除restaurantitems表中的所有记录。也就是说,如果餐厅被删除,我也希望其项目也被删除。

//Update.php
<?php
    $con = mysqli_connect("127.0.0.1","root","toor");
    mysqli_select_db($con, 'eatrebs');
    $id = isset($_POST['id']) ? $_POST['id'] : '';
    $name = isset($_POST['rname']) ? $_POST['rname'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $address = isset($_POST['address']) ? $_POST['address'] : '';
    if(isset($_POST['del']))
    {
        if ($sql = mysqli_prepare($con,"DELETE FROM restaurant where restaurant.rest_id = ?")){
            $sql -> bind_param("s",$id);
            $sql -> execute ();
            $sql -> close();
            header('refresh:1; url="index.php"');
        }
        else{
            echo"No update";
        }
    }
    else{
        if ($sql = mysqli_prepare($con,"UPDATE restaurant SET rest_name = ?,email=?, address=? WHERE rest_id = ?")){
            $sql ->bind_param("ssss", $name, $email, $address, $id);
            $sql -> execute();
            $sql -> close();
            header('refresh:1; url="index.php"');
        }
        else{
            echo "No update";
        }
    }




?>




//index.php

<html>
    <head>
         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.css">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="popup.css">
        <link rel="stylesheet" href="PopupSearch.css">
        <script src="popup.js"></script>
        <script src="PopupSearch.js"></script>
        <meta charset="UTF-8">
        <title>Owner's view</title>
    </head>
    <body>
        <?php
        //Connect to mysql db
        $con = mysqli_connect('127.0.0.1','root','toor');
        //select database
        mysqli_select_db($con,'eatrebs');
        //Select query
        $sql = "SELECT * FROM restaurant";
        //Execute query
        $records = mysqli_query($con,$sql);

        ?>
        <table>
            <h1 align="left">
                Restaurant Listings
                <button class="btn">
                    <i class="fa fa-bars"></i>
                </button>
            </h1>
            <Hr>
            <div id="Listingsblock">
            <thead>
            <tr>
            <div id="Listing_Labels">
            <th class="text-center">Restaurant Name</th>
            <th class="text-center">Email</th>
            <th class="text-center">Address</th>
            </tr>
            </div>
            </thead>
            <tbody>
        <?php
        while ($row = mysqli_fetch_array($records))
        {

            echo '<tr><form action="update.php" method="post">';
            echo"<input type=hidden name=id value='".$row['rest_id']."'></td>";
            echo "<td><input type = text name = \"rname\"  value = '".$row['rest_name']."'</td>";
            echo "<td><input type = text name = \"email\"  value = '".$row['email']."'</td>";
            echo "<td><input type = text name = \"address\"  value = '".$row['address']."'</td>";
            echo"<td><input type=submit value=\"Update\">";
            echo'<td><Button btn btn-primary btn-lg pull-left name=del>Delete</Button>';
            echo "</form>";

            echo '<form action="Menu/viewMenu.php" method="post">';
            echo"<input type=hidden name=iid value='".$row['rest_id']."'></td>";
            echo"<td><input type=submit value=\"View Menu\" name=viewMenu>";
            echo'</form></tr>';

        }
        $a = mysqli_num_rows($records);
        if($a==0){
            echo'<h1>No Records Available</h1>';

        }


        ?>
            </tbody>
            </table>
        <hr>
        <hr>
        </div>
        <!--Insert Records-->
        <h1 align="left">
            Create new Listing
            <button type="button" class="btn btn-default btn-sm" onclick="openForm()">
                <span class="glyphicon glyphicon-plus-sign"></span> Plus
            </button>
        </h1>
        <br>
        <!--create entry popup-->






        <div class="form-popup" id="myForm">
            <form action="create.php" class="form-container" method="post">
            <h1>Register</h1>

            <label for="rname2"><b>Restaurant Name</b></label>
            <input type="text" placeholder="Enter Restaurant Name" name="rname2" required>

            <label for="email2"><b>Email</b></label>
            <input type="text" placeholder="Enter email" name="email2" required>

            <label for="address2"><b>Address</b></label>
            <input type="text" placeholder="Enter Address" name="address2" required>

            <button type="submit" class="btn">Register</button>
            <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
          </form>

        </div>
  <hr>
        <hr>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

首先,您将丢弃数据库查询中的响应。

您应该捕获此错误并采取相应措施。 mysqli_stmt::execute()成功时返回true,失败时返回false。如果失败了,您可能应该使用其error()做些什么。通过显示一条错误消息来解释正在发生的事情,这将使您避免“我的页面刚刚刷新”。

第二,您的数据库正在保护您。它希望您的数据格式正确,并且,如果您删除另一条记录引用的记录,则不知道该怎么做。最安全的事情,在许多情况下实际上是正确的事情,是拒绝删除引用的记录。

不过,您可以更改此行为。 add your constraint时,您可以提供ON DELETE子句,例如

ALTER TABLE item
  ADD CONSTRAINT restaurant_fk
  FOREIGN KEY (rest_id)
  REFERENCES restaurant(id)
  ON DELETE CASCADE;

在这里,ON DELETE CASCADE告诉您的数据库,当其相应的item被删除时,应该删除restaurant

在添加此约束之前,您必须先删除旧约束。这取决于当前的配置方式,但是类似

ALTER TABLE item
  DROP FOREIGN KEY whatever_its_name_is;

应该可以解决问题。