christostsang
christostsang

Reputation: 1841

PMs - Deleting messages from outbox PHP/MYSQL

I am trying to create a basic PM system. I want it to send/receive messages. Also i want to keep a record of these messages by creating an inbox/outbox. In inbox i would like to be able to delete/respond to the message(s) and in outbox only to delete message(s).

I am having trouble deleting message(s) from OUTBOX. What happens is, when pressing DELETE button to delete message 1, it deletes the latest OUTBOX entry. For example, if i have message 1, message 2, message 3 and i press the DELETE button on message 1, is going to delete message 3.

The weird thing is that the exact same code for INBOX is working perfectly, even respond.

The messages table is consisted of the following columns: 1)message_id (PRIMARY INT) 2)sender 3)receiver 4)message 5)sent_deleted (default value = NO) 6)received_deleted (default value = NO) 7)send_date

Below is the code for OUTBOX:

<html>
<body>

<?php

//calling the variable using $_SESSION[]
session_start();
$sender = $_SESSION['USER'];
echo $sender;


//opening connection with database
require('C:\wamp\www\TEST\connect_db.php');

//to delete outbox message
if(isset($_POST['DELETE_MSG']))
{


$id = $_POST['DELETE_ID']; 


$query="UPDATE messages SET sent_deleted='YES' WHERE sender='$sender' AND     message_id='$id' ";
$results = mysqli_query($dbc,$query);

if(false ===$results)
{
    echo mysqli_error($dbc);
}

echo "Message succesfully deleted from your outbox<p>";
echo '<br>|<a href="main_page.php">HOME</a>|';

exit();

}

//searching for all messages that you have send a.k.a outbox and you havent deleted yet
$query = "SELECT * FROM messages WHERE sender='$sender' AND sent_deleted='NO' ";
$results = mysqli_query($dbc,$query);


if(false ===$results)
    {
        echo mysqli_error($dbc);
    }

$num = mysqli_num_rows($results);


$i=1; //initialize counter for outbox messages
$unread_outbox_id = array(); 


echo '<h1>OUTBOX</h1><br>'; 
if($num === 0) //check if the user has no outbox messages
{
echo "No OUTBOX messages!<br>";
echo '<br>|<a href="main_page.php">HOME</a>|';
exit();

}
else
{
while($row = mysqli_fetch_array($results,MYSQLI_BOTH))
    {

        $unread_outbox_id[$i][$row['message_id']] = $row['message_id'];


        echo "<p><font color='blue'><u><b>MESSAGE $i</b></u></font><br>";
        echo '<p><b><u>To</u></b>:' .$row['sender'].'<br>';
        echo '<b><u>From:</u></b>' .$row['receiver'].'<br>';
        echo '<b><u>Message:</u></b><br>' .$row['message'].'<br>';
        echo '<b><u>Date Received</u></b><br>' .$row['send_date'].'<br>';
    ?>


<form action="outbox.php" method="POST">
<input type="hidden" name="DELETE_ID" value="<?php echo $unread_outbox_id[$i]   [$row['message_id']]; ?>">
<input type="submit" name="DELETE_MSG" value="Delete message <?php echo $i; ?>">
</fomr>


<?php


    $i++;                           

    }


}


?>


</body>


<br>|<a href="main_page.php">Home</a>|

</html>

What i do, is that i set the correct message_id to the hidden input, and as soon as the user presses DELETE message 1, it jumps into the if(isset($_POST['DELETE_MSG'])) and executes the code. The code updates the sent_deleted field to 'YES' where message_id is equal to the correct message_id.

I cannot find the problem. The Delete message # always deletes the latest entry. Any thoughts?

Thanks in advance.

Upvotes: 0

Views: 385

Answers (1)

Wrikken
Wrikken

Reputation: 70520

</fomr> doesn't close a form, the browser / user-agent tries to cope, and sets ALL your submits & hidden inputs in one big form, making the last DELETE_ID the one that ends up in $_POST['DELETE_ID']. Moral of the story: validate your HTML, it's more important then you think :).

Upvotes: 1

Related Questions