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
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.
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... =/
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
<?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
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
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> <? }
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:
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?
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:
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 =/
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 ?
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 =/
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?
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 }