...

View Full Version : Resolved PHP Form - Delete Button



Dom Mv
11-18-2011, 01:29 AM
I have recently started to learn PHP and have come across a small problem that I can't resolve. I have created a page for sending a message (submitting data to the database) and a page for viewing (retrieving data from the database) the messages. What I would now like to do is create a delete button that removes individual messages from the database.

Here is what I have got so far:



//Connects to database:
<?php
include ('./connect.php');
?>




<?php

$db;

$messageInbox = mysql_query("SELECT * FROM messages");

while($inbox = mysql_fetch_array($messageInbox))
{
echo "
<hr>
<form action='./messageinbox.php' method='post'>
<table>
<tr>
<td>ID:</td>
<td>" . $inbox['id'] . "</td>
</tr>
<tr>
<td>Name:</td>
<td>" . $inbox['name'] . "</td>
</tr>
<tr>
<td>Subject:</td>
<td>" . $inbox['subject'] . "</td>
</tr>
<tr>
<td>Message:</td>
<td>" . $inbox['message'] . "></td>
</tr>
<tr>
<td colspan='2'><input type='submit' name='delete' value='Delete Message'></td>
</tr>
</table>
</form>
";
}

if(isset($_POST['delete']))
{
mysql_query("DELETE FROM messages");
}

mysql_close($con);


?>


I am able to deletes all entries, but not individual ones. I understand that the solution revolves around using the id, but I haven't managed to accomplish my goal as of yet.

Any help would be greatly appreciated. :)

mlseim
11-18-2011, 02:34 AM
Insert a hidden value that contains the id.
That satisfies the $_POST variable it sends to the script.




<?php

$db;

$messageInbox = mysql_query("SELECT * FROM messages");

while($inbox = mysql_fetch_array($messageInbox))
{
echo "
<hr>
<form action='./messageinbox.php' method='post'>
<input type='hidden' name='id' value='".$inbox['id']."'>
<table>
<tr>
<td>ID:</td>
<td>" . $inbox['id'] . "</td>
</tr>
<tr>
<td>Name:</td>
<td>" . $inbox['name'] . "</td>
</tr>
<tr>
<td>Subject:</td>
<td>" . $inbox['subject'] . "</td>
</tr>
<tr>
<td>Message:</td>
<td>" . $inbox['message'] . "></td>
</tr>
<tr>
<td colspan='2'><input type='submit' name='delete' value='Delete Message'></td>
</tr>
</table>
</form>
";
}

if($_POST['delete'] == "Delete Message")
{
mysql_query("DELETE FROM messages WHERE id = '".$_POST['id']."' ");
}

mysql_close($con);


?>

Dom Mv
11-18-2011, 08:14 AM
That's done the trick! Thanks for your help. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum