thenoob
thenoob

Reputation: 67

delete a row based on id

I am trying to delete a row based on its ID

HTML:

<form action=""  method="post">
<input type="submit" name="delete" value=" delete "/>
</form>

PHP:

        $u = $_REQUEST['edit']; // contains the ID of row to be deleted

        if(isset($_POST['delete'])){
            $db->exec("DELETE FROM infos WHERE id = '$u'");
        }   

Nothing happens when the delete button is pressed. How can I fix this?

Upvotes: 0

Views: 162

Answers (2)

lalithkumar
lalithkumar

Reputation: 3540

<form action=""  method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form> 

After that try this

$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if($u){
   $db->exec("DELETE FROM infos WHERE id = '$u'");
}

Apparently when you are submitting only you posting data to php file.So you can check like this is enough.you told delete not happening so that i am posting this answer

Upvotes: 1

Barmar
Barmar

Reputation: 780889

You need an edit input whose value is the ID your want to delete.

<form action=""  method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>

Replace $id with the actual variable you use in the script that creates the form.

Upvotes: 3

Related Questions