user1965409
user1965409

Reputation: 1

How To Delete Row in Database With Delete Button

I am trying add a "Delete" button in my application. The button's functionality is to remove the database row when it is clicked. I guess I will need to create something like delete.php and link the button to it. But I have no idea how to do it. Can anyone help?

Below is my code:

<table id="edit_accounts" class="tablesorter">
<thead>
<tr><?php
while($v=mysql_fetch_field($result)) if($v->name!="paid_for_year_date" && $v->name!="approved"){
?><th class="header"><?php echo display_version($v->name);?></th>
<?php
}
?>
<th class="header">Actions</td>
</tr>
</thead>
<?php
while($row=mysql_fetch_assoc($result)){
$row["category"]=$categories[$row["category"]]["category_name"];
$pfydate=$row["paid_for_year_date"];
unset($row["paid_for_year_date"]);
$extra_link="";
if($pfydate==$row["join_date"]){
    $extra_link="<br/><a href='mark_as_paid.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Mark as Paid</a>";
}
if($row["approved"]==0){
    $extra_link.="<br/><a href='approve.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Approve</a>";
}
unset($row["approved"]);

?>
<tr><td><?php echo implode("</td><td>",$row);?></td><td><a href="edit_account.php?account=<?php echo $row["account_id"];?>">Edit Account</a><br><a href="view_transactions.php?account=<?php echo $row["account_id"];?>">Edit Transactions</a><br/><a href="delete.php?account_id=".$row[account_id"]<?php echo $row["account_id"];?>">Edit Account</a><?php echo $extra_link;?></td></tr>
<?php 
}
?>
</table>

Upvotes: 0

Views: 904

Answers (2)

Jigar Patel
Jigar Patel

Reputation: 4615

You didn't mention how do you want to update your page. There are essentially two way to refresh the page.

  1. Refresh the entire page.

In this you will call your delete.php with some parameters. In delete.php you perform the delete operation based on given parameter and then send it back to your code responsible for genering your page. The newly generated page will not have deleted row.

  1. AJAX

If you are using AJAX based call to delete item This becomes little bit more complex on browser related code. - You pass id/parameters to delete.php via AJAX call. - delete.php deletes row and sends status message back to browser. - You javascript handles the response and performs required DOM manipulation to delete row from table in case of successful operation.

Upvotes: 0

Edwin Alex
Edwin Alex

Reputation: 5108

Follow these steps

1) You need to create a file deleteFile.php (or) you can create a single file and do all the operations like insert, update and delete by using if conditions.

2) Then, you should pass the row id or some identifier to the page to identify which row has to be deleted.

3) In that, you should write a query to delete the row by using this identifier.

4) Then you can give an alert msg and redirect into the page.

5) In the case of single file, you need to send an operation type like delete with the id to the page

Upvotes: 1

Related Questions