Help: Simple delete message button...

Discussion in 'PHP' started by Skillman13, Dec 15, 2009.

  1. #1
    I'm making a quick/simple messaging system for users, and I cant get it to delete messages... The problem is as it prints a message it changes the $messageid so it always deletes the last one,

    Can anyone fix this...

    echo 'Your messages:'. '<br>';

    $query = ("SELECT * FROM `Messages` WHERE Reciever = '$username' ORDER BY `Time` DESC");

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)) {

    $messageid = $row['MessageId'];

    echo $row['Sender'] . ": ". $row['Message']. "<br>";

    ?>

    <form id="form1" name="form1" method="post" action="">

    <input type="submit" name="Submit" id="Submit" value="Submit" />

    </form>

    <?

    }
    if ($_POST['Submit'] == "Submit"){
    mysql_query("DELETE FROM `Messages` WHERE `MessageId` = '$messageid'");
    }
    }

    ?>


    Or give me a whole new way of doing it? :)

    Thanks alot,

    James
     
    Skillman13, Dec 15, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    code snippet seems incomplete and more over, anyways, you are executing delete query after while loop which is why it is deleting only last message.
    More delete query inside while loop and it should delete them all.
     
    mastermunj, Dec 15, 2009 IP
  3. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No i want the button to come after every message, and to delete that message only. -Currently the button appears but no matter which one you click, it will delete the last message... =/
     
    Skillman13, Dec 15, 2009 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    You need a hidden field where you can store the id value. Then use that to delete that one from the database in the delete query.

    Glen
     
    HuggyStudios, Dec 15, 2009 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    
    
    <?php
    if(isset($_POST['delete'])) {
    $query="DELETE FROM Messages WHERE messageid='{$_POST['delete']}'";
    $result=mysql_query($query);
    //header("Location: /"); //EDIT THIS TO YOUR NEEDS
    }
    ?>
    
    
    PHP:
    ADD this to your echo, so it appears after each message
    
    
    <form method="post" action=""><input type="image" src="images/delete.png" name="delete" value="' . $row['messageid'] . '" /></form>
    
    
    Code (markup):
    I have attached the delete.png image, also edit or alter the Location part of the function above or it will seem to do nothing, refreshing the page will show the message has been removed
     
    Last edited: Dec 15, 2009
    MyVodaFone, Dec 15, 2009 IP
  6. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Wow, great thanks alot :) Still got the problem, since it assigns a new $messageid to each one '$messageid = $row['MessageId'];' The one the button deletes is the last one, since it deletes $messageid, -which will be the last assigned =/

    Maybe an array can fix this, or another way, anyone got an idea? :) Thanks,

    James
     
    Skillman13, Dec 15, 2009 IP
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    Put a button in for each entry?

    Glen
     
    HuggyStudios, Dec 15, 2009 IP
  8. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    There is a button for each message, -its on a loop, read my code please :)


    while($row = mysql_fetch_array($result)) {

    $messageid = $row['MessageId'];

    echo $row['Sender'] . ": ". $row['Message']. "<br>";

    ?>

    <form id="form1" name="form1" method="post" action="">

    <input type="submit" name="Submit" id="Submit" value="Submit" />

    </form>

    <?

    }
     
    Skillman13, Dec 15, 2009 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    So what your saying is the database doesnt hold ID's at all ?

    What about using Sender instead ?

    
    
    <?php
    if(isset($_POST['delete'])) {
    $query="DELETE FROM Messages WHERE Sender='{$_POST['delete']}'";
    $result=mysql_query($query);
    //header("Location: /"); //EDIT THIS TO YOUR NEEDS
    }
    ?>
    
    
    PHP:
    
    
    echo $row['Sender'] . ": ". $row['Message']. " <form method="post" action=""><input type="image" src="images/delete.png" name="delete" value="' . $row['Sender'] . '" /></form><br>";
    
    
    PHP:
     
    MyVodaFone, Dec 15, 2009 IP
  10. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It does... What i'm saying, is as the code 'loads'/gets a message from the database it also gets the messageid, and $messageid is the that messageid...

    NOW...

    When a second message is loaded, the database gets the new messageid, so $messageid is the NEW messageid =/

    So no matter which button you press it will always delete the last message, -the $messageid.

    Any fixes? :)
     
    Skillman13, Dec 15, 2009 IP
  11. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #11
    I dont know why it not working then....

    With what I provided your code
    
    while($row = mysql_fetch_array($result)) {
    
    $messageid = $row['MessageId'];
    
    echo $row['Sender'] . ": ". $row['Message']. "<br>";
    
    ?>
    
    <form id="form1" name="form1" method="post" action="">
    
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    
    </form>
    
    <?
    
    } 
    
    
    PHP:
    Should become this:

    
    if(isset($_POST['delete'])) {
    $query="DELETE FROM Messages WHERE messageid='{$_POST['delete']}'";
    $result=mysql_query($query);
    //header("Location: /"); //EDIT THIS TO YOUR NEEDS
    }
    
    while($row = mysql_fetch_array($result)) {
    
    echo $row['Sender'] . ": ". $row['Message']. " <form method="post" action=""><input type="image" src="images/delete.png" name="delete" value="' . $row['messageid'] . '" /></form><br>";
    
    } 
    
    
    PHP:
     
    Last edited: Dec 15, 2009
    MyVodaFone, Dec 15, 2009 IP
  12. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No doesn't work, -i put my isset behind the form before, and thats how i got the problem i was just describing, now i put it before like you just advised, now it does nothing. So i'll keep it after the form, still need to fix the problem =/
     
    Skillman13, Dec 15, 2009 IP
  13. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #13
    When you say it does nothing, did you refresh the page to see if the message was deleted, also load the page and view the source code, does it show a different messageid number on the form to delete ?
     
    MyVodaFone, Dec 15, 2009 IP
  14. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I refreshed, plus i put the header in (uncommented)...

    Erm ok i'll check now...

    Your messages:<br><form method="post" action="">
    Test: Hello <input type="image" src="delete.png" name="delete" value="' . . '" /></form>

    <form method="post" action="">
    Test: Hi <input type="image" src="delete.png" name="delete" value="' . . '" /></form>

    So no =/
     
    Skillman13, Dec 15, 2009 IP
  15. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ok i got it so it says values now, -echo then $row['MessageId']
     
    Skillman13, Dec 15, 2009 IP
  16. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #16
    Ok sorry my bad $row['messageid'] should be $row['MessageId']
     
    MyVodaFone, Dec 15, 2009 IP
  17. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hmm, it could work if on

    if(isset($_POST['delete'])) {
    ****Here***
    mysql_query("DELETE FROM `Messages` WHERE `MessageId` = '$messageid'");

    We turned $messageid into the value of the button, how can we do that? :)
     
    Skillman13, Dec 15, 2009 IP
  18. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Works now... lmao Simple: $messageid = $_POST['delete'];
    (At the end)

    Reference code:


    while($row = mysql_fetch_array($result)) {
    $messageid = $row['MessageId'];
    ?>
    <form method="post" action="">
    <?
    echo $row['Sender'] . ": ". $row['Message'] . " ";
    ?>
    <input type="image" src="delete.png" name="delete" value="<? echo $row['MessageId'] ?> " /></form>

    <?

    }
    if(isset($_POST['delete'])) {
    $messageid = $_POST['delete'];
    mysql_query("DELETE FROM `Messages` WHERE `MessageId` = '$messageid'");
    header("Location: /zombie/messages.php "); //EDIT THIS TO YOUR NEEDS
    }
     
    Skillman13, Dec 15, 2009 IP