Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Delete inbox not working properly

Tags:

php

mysql

inbox

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 :

$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

$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 ?

like image 852
user3474238 Avatar asked Nov 10 '22 10:11

user3474238


1 Answers

instead of directly evaluating a variable which got filled by data that comes from POST,GET global variables, use isset() function first to check weather they got any value, and then the return value of this isset function can be given to if for evaluation.

eg : $inboxbtn = $_POST['deleteinbox'];
$outboxbtn = $_POST['deleteoutbox'];
if(isset($inboxbtn)){
     $selectall = $_POST['selectall'];
    if(isset($selectall)){

 }
}

i think u got problem in this, check this out .... http://in2.php.net/isset https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

like image 136
Shashidhar Gr Avatar answered Nov 14 '22 22:11

Shashidhar Gr