perky416 Posted March 16, 2011 Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230855-messages-not-being-removed-when-page-refreshed-first-time/ Share on other sites More sharing options...
perky416 Posted March 16, 2011 Author Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230855-messages-not-being-removed-when-page-refreshed-first-time/#findComment-1188402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.