Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    England
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile PHP Form - Delete Button

    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:

    PHP Code:
    //Connects to database:
    <?php
    include ('./connect.php');
    ?>
    PHP Code:
    <?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.
    Last edited by Dom Mv; 11-19-2011 at 09:08 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Insert a hidden value that contains the id.
    That satisfies the $_POST variable it sends to the script.

    PHP Code:

    <?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);


    ?>

  • Users who have thanked mlseim for this post:

    Dom Mv (11-18-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    England
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That's done the trick! Thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •