Jump to content

Messages not being removed when page refreshed first time


perky416

Recommended Posts

Hi guys,

 

On my website i have a message inbox, and the code below is used to display and delete messages. When i tick the checkbox and click the delete button, the message gets deleted. The page appears to refresh after the submit button is clicked, however the message remains in the inbox, i have to refresh the page a second time for the message to disappear. 

 

Im trying to get it that when i press the submit button and the page reloads, the message disappears. Does anybody know of a technique that im not using or does anybody know if the code im using is wrong?

 

<?php
$messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'");
if (mysql_num_rows($messages_query) > 0)
{
	echo "<form method='POST' action='inbox.php'>";
	while ($messages_row = mysql_fetch_array($messages_query))
	{	
   			if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; }
		$message_id = $messages_row['id'];
   			echo "<a href='message.php?id=$message_id'>";
		echo "From: " . $messages_row['sender'];
		echo "Subject: " . $messages_row['subject'] . "<br />";
		echo "</a>";
		echo "<input type='checkbox' name='message[]' value='$message_id' />";
		if ($messages_row['message_read'] == 0) { echo "</div>"; }
	}
}	
else
{
	echo "No messages";
}

if ($submit)
{
	foreach ($_POST['message'] as $msgID)
	{
			mysql_query("DELETE FROM messages WHERE id='$msgID'");
	}
}
?>
<html>
<input type="submit" name="submit" value="Delete" />
</form>
</html>

 

Many thanks

Link to comment
Share on other sites

Hi guys

 

Just for anybody else in the future that comes across this thread i managed to figure out the problem.

 

I figured that the data was being displayed before the delete query was processed, i simply moved the following towards the top of the code, my theory is that now by the time the data gets displayed the query has already been processed in the database.

 

if ($submit)
{
	foreach ($_POST['message'] as $msgID)
	{
			mysql_query("DELETE FROM messages WHERE id='$msgID'");
	}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.