SQL 删除功能在 table 中不起作用

SQL Delete feature does not function in table

我有三个相互连接的文件,我的问题是我的删除功能似乎不起作用。我想知道我的 "public function Delete_Lease($db) {" 代码中缺少什么,如下所示。基本上,我用一个 for each 来显示一个 table,编辑没有问题,但删除只是停留在同一页上,没有任何效果。

Table 姓名: for_lease

值: leaseid,lease_type,lease_name,lease_address,lease_price,lease_condition,lease_description、featured_photo、创建日期

for_lease.php

<?php
  session_start();
  require_once('for_lease.vc.php');
?>

 <?php foreach($lstProperty as $rowProperty) { ?>
       <tr align="center">
         <td>
          <a href="for_lease_edit.php?i=<?php echo($rowProperty['leaseid']); ?>"><input type="submit" class="btn bg-color-blue color-white form-control" name="edit" value="EDIT"></a>
         </td>

         <td>
           <?php
             echo($rowProperty['lease_name']);
           ?>
         </td>

         <td>
           <?php
             echo($rowProperty['lease_address']);
           ?>
         </td>

         <td>
           <?php
             echo($rowProperty['lease_type']);
           ?>
         </td>

         <td>
           <?php
             echo 'PHP'.' '.number_format(($rowProperty['lease_price']));
           ?>
         </td>

         <td>
           <?php
             echo($rowProperty['lease_condition']);
           ?>
         </td>

         <td>
           <?php
             echo( date("Y-m-d", strtotime($rowProperty['createddate']) ));
           ?>
         </td>

         <td>
           <a href="for_lease.php?delete=<?php echo($rowProperty['leaseid']); ?>" onclick="return confirm('Are you sure?');"><input type="submit" class="btn bg-color-red color-white form-control" name="delete" value="DELETE"></a>
         </td>
       </tr>
 <?php } ?>

for_lease.vc.php

<?php
  $routePath = "../";

  require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

  require_once($routePath . "_mc/Property.mc.php");

  $mcProperty = new Property_MC();

  $lstProperty = $mcProperty->SelectObj_ByLeaseId($db);

  if (isset($_GET['delete'])){
    $rowProperty = $mcProperty->Delete_Lease($db);
  }
?>

Property.mc.php

<?php
Class Property_MC {

  public function SelectObj_ByLeaseId($db) {
  $stmt = $db->prepare(
    " SELECT leaseid, lease_type, lease_name, lease_address, lease_price, lease_condition, lease_description, createddate
      FROM for_lease"
  );

  $stmt->execute();
  $row = $stmt->fetchAll(PDO::FETCH_ASSOC);

  return $row;
  }

  public function Delete_Lease($db) {
  $stmt = $db->prepare(
    " DELETE * FROM
        for_lease
      WHERE
        leaseid = :leaseid "
  );
  }

} ?>

您没有执行 sql 语句,您只是在准备它。此外,您的代码设置方式,您需要将 leaseid 传递给您的删除功能。

试试这个。

for_lease.vc.php

<?php
  $routePath = "../";

  require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

  require_once($routePath . "_mc/Property.mc.php");

  $mcProperty = new Property_MC();

  $lstProperty = $mcProperty->SelectObj_ByLeaseId($db);

  if (isset($_GET['delete'])){
    $rowProperty = $mcProperty->Delete_Lease($db, $_GET['delete']);
  }
?>

Property.mc.php

 public function Delete_Lease($db, $leaseid) {
  $stmt = $db->prepare(
    " DELETE FROM
        for_lease
      WHERE
        leaseid = :leaseid "
  );

  $stmt->execute([':leaseid' => $leaseid]);  

  }

Delete_Lease 缺少绑定和执行以及要删除的租约 ID。 用于删除的 sql 不需要“*”。

Property.mc.php

public function Delete_Lease($db, $leaseid) {
    $stmt = $db->prepare(
        "DELETE FROM
        for_lease
        WHERE
        leaseid = :leaseid "
        );  
    $stmt->execute(['leaseid' => $leaseid]);
}

for_lease.vc.php

if (isset($_GET['delete'])){
    $rowProperty = $mcProperty->Delete_Lease($db, $_GET['leaseid']);
}

修复删除按钮覆盖 for_lease.php

中的删除 ID
<td>
    <a href="for_lease.php?leaseid=<?php echo($rowProperty['leaseid']); ?>" onclick="return confirm('Are you sure?');"><input type="submit" class="btn bg-color-red color-white form-control" name="delete" value="DELETE"></a>
</td>