Search code examples
phpsqlpdosql-delete

SQL Delete feature does not function in table


I have three files connecting to each other, my issue is that my delete function does not seem to be working. I'd like to know what is missing in my "public function Delete_Lease($db) {" the codes are shown are shown below. Basically, I use a for each to display a table, the edit works with no problem but the delete just stays on the same page and has no effect.

Table Name: for_lease

Values: leaseid, lease_type, lease_name, lease_address, lease_price, lease_condition, lease_description, featured_photo, createddate

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

} ?>

Solution

  • Delete_Lease is missing binding and execution as well as what lease id to delete. the sql for delete does not need the "*".

    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']);
    }
    

    fix delete button that overwrites delete id in for_lease.php

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