Jump to content

How to make delete button functional in my guestbook


Umaid123

Recommended Posts

I want to make delete button to delete the record from mysql database using PHP. I have made the button in HTML and apply some formatting as below, but i am not coming up on actual logic, so plz help.

 

$comment = $_POST[txt_comment];

$comment_len = strlen ($comment);

$delete = $_POST["del"];

if ( $delete > $comment_len )

{

function foo($comment)

{

unset($comment);

}

}

else

{

echo ($comment);

}

 

<input type="submit" value ="Delete" name = "del">

Basically you need to get the ID of post attached to the form. When your code grabs the info from the database, you then need to add a hidden field to the form and have it hold the id of the post.

 

Then when that delete button is hit, you will run a query to delete.

 

<input type="hidden" name="commentId" value="<?php echo $commentId; ?>" />

 

<input type="submit" value ="Delete" name = "del">

 

<?php

if(isset($_POST['del']) && isset($_POST['commentId']))
{
  $id = $_POST['commentId'];
  $query = "DELETE FROM tableName WHERE fieldName = '$id'";
  $result = mysql_query($query);
}

?>

 

Something like that is what you need to do. You will nee to change things to your actual field and table names, but that is the principle.

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.