donglu9978 2014-01-25 00:06
浏览 45

PM - 从发件箱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.

  • 写回答

1条回答 默认 最新

  • drn9573 2014-01-25 00:16
    关注

    </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 :).

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行