Page 1 of 1

Strange PHP problem

Posted: Mon Jul 12, 2010 3:21 pm
by jasondrawmer
Hi All,

This is the first real stumbling block i've come across in PHP and i've been using it for years - that's how confused I am with this problem - maybe i'm just tired and can't see it!!!

I am currently developing a personal messaging part of my site, and when reading a message, have a delete button. If the message is NOT in deleted items, it puts it in there. If it is in deleted items, it deletes the message altogether. All fine there.

I have the following code at the top of my page when the delete button is pressed:

Code: Select all

if (isset($_POST['delete']))
    {
        $TempMessage = New Message($id);
        
        $qry = "";
        
        if ($TempMessage->mFolder->mfID == 3)
        {
            $qry = "DELETE FROM user_message WHERE usme_id = '".$id."'";
        }
        else
        {
            $qry = "UPDATE user_message SET mefo_id = 3 WHERE usme_id = '".$id."'";
        }
        
        $qry_res = @mysql_query($qry, $db);
        
        if ($qry_res != false)
        {
            Header("Location: messages.php");
        }
    }
directly underneath that I have teh following

Code: Select all

$ThisMessage = New Message($id);
    
    if ($ThisMessage->mUserID != $_CurrentUser->uID)
    {
    	Header("Location: ../400.shtml");
    }
the strange thing is that when an item is UPDATED and not deleted, everything is fine - the header changes to messages.php and runs as expected. The strange scenario occurs when I try to delete a deleted item (the delete statement). The message IS deleted successfully, however the Header doesn't seem to go through properly, and the $ThisMessage etc. code runs, resulting in an error, and the header changing to 400.shtml.

Sorry if you don't understand my issue - I will try and delve further if necessary. It's really stumped me!!

Thanks in advance.

Re: Strange PHP problem

Posted: Tue Jul 13, 2010 9:47 am
by Jade
The problem is that you're trying to load and reference the deleted ID. Try doing this:

Code: Select all

if (isset($_POST['delete']))
    {
        $TempMessage = New Message($id);
       
        $qry = "";
       
        if ($TempMessage->mFolder->mfID == 3)
        {
            $qry = "DELETE FROM user_message WHERE usme_id = '".$id."'";
            unset($id); //this message is gone, make sure we don't try to load it again later on
        }
        else
        {
            $qry = "UPDATE user_message SET mefo_id = 3 WHERE usme_id = '".$id."'";
        }
       
        $qry_res = @mysql_query($qry, $db);
       
        if ($qry_res != false)
        {
            Header("Location: messages.php");
        }
    }
 
if ($id) //the message exists
{
$ThisMessage = New Message($id);
   
    if ($ThisMessage->mUserID != $_CurrentUser->uID)
    {
        Header("Location: ../400.shtml");
    }
}