Hi!

I want to update or delete a record in database whose values will be taken from form.
I tell you the scenario....


First, I created a form for data input which i then saved in database . Then i get the results from database in the form of table. Now i put an edit link in front of each row to edit or delete any of them. When i click on edit link im forwarded to the inital form with the values of row i selected in the table. Now here i want to make changes in the desired fields and save it. In other words want to update a record. I have created two button for updation or deletion the selected record in the form where i get the values. How can i do this through these buttons? how can buttons be used for updating and deleting these.

I hope i have not make you confused...

Thanks

You could use the $_POST var in your form handling code. Make the submit button (id = "delete") and submit button (id="update"). Also ensure that you have a hidden input with the id of the of record (or user), eg:

<input type="hidden" id="user" value ="<?php echo $data['id'];?>" />

Your handling routine will now accept the id from whichever button ($_POST or $_POST) made the submission. The id of the user will also be passed, $_POST.

$id = $_POST['user'];
if(isset($_POST['delete'])){
   $query = "DELETE FROM yourtable WHERE user_id = '{$id}'";
   ....
}else{
  $field_x = addslashes(htmlentities($_POST['field_x']));

//where field_x is the id of your form field
  $query = "UPDATE yourtable SET myfield = '{$field_x}' WHERE user_id = '{$id}'";
  ...
}

quick and dirty - but gives you an idea.

This article has been dead for over six months. Start a new discussion instead.