0

I have an inbox code for deleting messages.

If I select one single message it deletes all of them.

How can I fix this ?

Here is my code for delete_message.php :

<?php
$inboxbtn = $_POST['deleteinbox'];
$outboxbtn = $_POST['deleteoutbox'];

if ($inboxbtn)
    {
    $selectall = $_POST['selectall'];
    if ($selectall)
        {
        $query = mysql_query("SELECT * FROM messages WHERE to_user='$user'");
        while ($row = mysql_fetch_assoc($query))
            {
            mysql_query("UPDATE messages SET to_delete='1' WHERE to_user='$user'");
            }

        echo "All messages have been deleted.";
        }
      else
        {
        $query = mysql_query("SELECT * FROM messages WHERE to_user='$user'");
        while ($row = mysql_fetch_assoc($query))
            {
            $msg_id = $row['id'];
            $value = "cb" . "$msg_id";
            $checkbox = $_POST[$value];
            if ($value)
                {
                mysql_query("UPDATE `messages` SET `to_delete`='1' WHERE `to_user`='$user' AND `id`='$msg_id'");
                }
            }

        echo "The selected messages have been deleted.";
        }
    }
elseif ($outboxbtn)
    {
    $selectall = $_POST['selectall'];
    if ($selectall)
        {
        $query = mysql_query("SELECT * FROM messages WHERE from_user='$user'");
        while ($row = mysql_fetch_assoc($query))
            {
            mysql_query("UPDATE messages SET from_delete='1' WHERE from_user='$user'");
            }

        echo "All messages have been deleted.";
        }
      else
        {
        $query = mysql_query("SELECT * FROM messages WHERE from_user='$user'");
        while ($row = mysql_fetch_assoc($query))
            {
            $msg_id = $row['id'];
            $value = "cb" . "$msg_id";
            $checkbox = $_POST[$value];
            if ($value)
                {
                mysql_query("UPDATE messages SET from_delete='1' WHERE to_user='$user' AND id='$msg_id'");
                }
            }

        echo "The selected messages have been deleted.";
        }
    }
  else echo "Choose a message to delete.";
?>

And here is the code in inbox.php that has the checkboxes

<?php
$query = mysql_query("SELECT * FROM messages WHERE from_user='$user' AND from_delete='0' ORDER BY id DESC");
$numrows = mysql_num_rows($query);

if ($numrows != 0)
    {
    echo "<form action='delete_message.php' method='POST'>";
    echo "<div class='messages'>
        <div class='leftside'><input type='checkbox' name='selectall'><input type='submit' name='deleteoutbox' value='Delete' class'button'></div>
        <div class='rightside'>Date</div>
        Subject And Message
        <div class='clear'></div>
        <hr>
        </div>";
    while ($row = mysql_fetch_assoc($query))
        {
        $msg_id = $row['id'];
        $msg_to_user = $row['to_user'];
        $msg_to_id = $row['to_id'];
        $msg_from_user = $row['from_user'];
        $msg_from_id = $row['from_id'];
        $msg_subject = $row['subject'];
        $content = nl2br($row['content']);
        $msg_date = $row['date'];
        $msg_from_delete = $row['from_delete'];
        $msg_to_delete = $row['to_delete'];
        if (!$msg_from_delete)
            {
            echo "<div class='messages'>";
            echo "<div class='leftside'>
        <input type='checkbox' name='cb$msg_id' value='$msg_id'>
        <a href='profile.php?id=$msg_to_id' target='_blank'>$msg_to_user</a>
        </div>";
            echo "<div class='rightside'>$msg_date</div>";
            echo "<div id='center' style='margin-left:150px; margin-right:150px;'>

        <span class='toggle'><a href='#' onClick='return false'>$msg_subject</a></span>
        <div class='hiddenDiv'>
        <br /><hr>
        <b>$smiles </b>
        <br /><br />


        </div>
        </div>";
            echo "<div class='clear'>";
            echo "<br /><br /><hr>";
            echo "</div></div>";
            }
        }

    echo "</form>";
    }
  else echo "You Have No Messages In Your Outbox"
?>

Then for the inbox messages it is the same as the outbox but in the inbox form.

How can I fix this ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Input Output elements on the forms are missed – Smash Mar 30 '14 at 06:34
  • What do you mean? Where would they go? – user3474238 Mar 30 '14 at 06:37
  • they're definitely on Alaska 100% – Smash Mar 30 '14 at 06:41
  • What?!??? please can you explain clearly. – user3474238 Mar 30 '14 at 06:45
  • 1
    There are several things wrong with your code. First, in the `else` block, you are fetching from the messages table, in the `while` loop you are checking that `id` against `$_POST` which is wrong. What you should ideally be doing is, use a foreach loop to iterate through the checked `ids` and update. Where are you getting the `$user` from anyway? You should also check which part of your `if` block is actually being executed. – AyB Mar 30 '14 at 06:55
  • $user is a session that stores the currently logged in users username. I am still confused as to what you mean. Could you possibly put the solution as an answer so I can mark it correct? – user3474238 Mar 30 '14 at 06:58

1 Answers1

2

The below code is for outbox.php since that's what you have pasted.

First, change your checkbox from:

<input type='checkbox' name='cb$msg_id' value='$msg_id'>

To something like:

<input type='checkbox' name='outbox_ids[]' value='$msg_id'>

And the elseif for the outbox will be:

elseif ($outboxbtn)
    {
    $selectall = $_POST['selectall'];
    if ($selectall)
        {
        $query = mysql_query("SELECT * FROM messages WHERE from_user='$user'");
        while ($row = mysql_fetch_assoc($query))
            {
            mysql_query("UPDATE messages SET from_delete='1' WHERE from_user='$user'");
            }

        echo "All messages have been deleted.";
        }
      else
        {
          if(isset($_POST['outbox_ids']){
            $outbox_msg_ids = array_map('mysql_real_escape_string', $_POST['outbox_ids']);
            //all the checked msg id are now stored in $outbox_msg_ids,
            //we will loop through the values and pass it to the update query
            foreach($outbox_msg_ids as $msg_id){
              mysql_query("UPDATE messages SET from_delete='1' WHERE to_user='$user' AND id='$msg_id'");
            }
            echo "The selected messages have been deleted.";
          } //isset if block ends
        }
    }

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
AyB
  • 11,609
  • 4
  • 32
  • 47
  • Using this for inbox: http://pastebin.com/xpYdVZcA I get these errors: http://gyazo.com/461b70a4700a5b5c3ce579fe29bda4a8.png Any idea??/ – user3474238 Mar 30 '14 at 07:41
  • @user3474238 Did you change the name of `input` for inbox.php? – AyB Mar 30 '14 at 07:43
  • @user3474238 Can you do a `print_r($_POST['inbox_ids']);` before the `array_map()` statement and see if it outputs anything? – AyB Mar 30 '14 at 07:51
  • Nothing. I put that code before the array map line but there was no difference. – user3474238 Mar 30 '14 at 07:53
  • @user3474238 It's probably because you are not selecting any message to delete. So when nothing is checked, it throws the error. I have edited to code to check if anything has been set. – AyB Mar 30 '14 at 07:56
  • Ok, I added that with an error but the error displayed even when I selected messages – user3474238 Mar 30 '14 at 08:05
  • @user3474238 Could you please paste the result of `print_r($_POST['inbox_ids']);` again after you have selected messages? – AyB Mar 30 '14 at 08:09
  • @user3474238 Something is not right with your HTML. Could you do a pastebin of your HTML? :3 – AyB Mar 30 '14 at 08:15
  • Sure, here is what I used for the check box: http://pastebin.com/XE2FAnPy And here is the inbox code again: http://pastebin.com/BPw6QVNu – user3474238 Mar 30 '14 at 08:17
  • @user3474238 You need to check whether the inputs are closed inside `
    ` tags and whether the `submit` button name is `name='deleteinbox'`. Btw you are missing a `{` after `else` before the `isset` line. And there is a repeating `{` after the `isset` line.
    – AyB Mar 30 '14 at 08:18
  • That solved it but there is another problem. It won't delete multiple messages, just do first one I selected. This is what the print_r($_POST['inbox_ids']); printed "Array ( [0] => 161 )" – user3474238 Mar 30 '14 at 08:23
  • ??? When I did that code, it only deleted one message instead of all the ones I selected. The print thing that was added earlier only showed: Array ( [0] => 161 ) – user3474238 Mar 30 '14 at 08:33
  • @user3474238 That isn't supposed to happen. Could you do a paste bin of your complete `inbox.php`? – AyB Mar 30 '14 at 08:34
  • The entire thing is massive, here is the entire part of the inbox part: http://pastebin.com/cXEmiEkP – user3474238 Mar 30 '14 at 08:38
  • @user3474238 The comments is getting long. Please click on **[this link](http://chat.stackoverflow.com/rooms/50352/extended-chat)** to join a chatroom. – AyB Mar 30 '14 at 09:09