0

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>
1
  • The mysql_ library is deprecated. You should look at moving to mysqli_ or PDO.
    – user985189
    Jan 10, 2013 at 4:03

2 Answers 2

1

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

0

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.

1
  • Thanks guys will let you know how it goes Jan 10, 2013 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.