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

时间:2019-05-26 22:01:11

标签: php sql pdo sql-delete

我有三个相互连接的文件,我的问题是我的删除功能似乎无法正常工作。我想知道“公共函数Delete_Lease($ db){”中缺少的内容,下面显示了代码。基本上,我每个人都使用a来显示表格,编辑工作没有问题,但是删除仅停留在同一页面上,没有任何作用。

表名: for_lease

值::租约ID,租赁类型,租赁名称,租赁地址,租赁价格,租赁条件,租赁描述,精选照片,创建日期

table 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 "
  );
  }

} ?>

2 个答案:

答案 0 :(得分:0)

您不执行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]);  

  }

答案 1 :(得分:0)

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>