开发者

What am I doing wrong? Messages won't delete

Ive been working on my delete.php file to delete selected messages for literally hours on end without any headway. How would I delete messages with the codes I have set up?

Heres the code to how my delete.php is set up right now

<?php
    session_start();
    header("Location:inbox.php");

    $user = $_SESSION['username'];

    include 'connect.php';


   foreach($_POST['messages'] as $num => $messages_id)

        mysql_query("DELETE FROM messages WHERE messages_id='$messages_id' AND to_user='$user'");

    ?> 

Here's the code to my entire inbox.php

<?php
require "connect.php";

echo "<hr><br><div id='mail'>";

// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE 

to_user='$userfinal' ORDER BY messages_id DESC") or die(mysql_error());
$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal' 

ORDER BY messages_id DESC") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';
for($count = 1; $count <= $num_messages; $count++)
{
    $row = mysql_fetch_array($get_messages2);
    $fromuser = $row['from_user'];
    $messageid = $row['messages_id'];
    $messagetitle = $row['message_title'];
    $messageread = $row['message_read'];
    $messagedate = $row['message_date'];



    echo " <form name='send' method='post' action='delete.php'><input type='checkbox' name='delete' value='$messageid'></td><font face='helvetica'><font size='3'>$fromuser</font></font> &nbsp; &开发者_Python百科;bull; &nbsp;  <div align='right'><a href='read_message.php?messages_id=$messageid'>$messagetitle</a></div>
<center><hr></center>


";
}
?>
<input type="submit" name="Submit" value="Delete Selected">
</table>
</div>

Please help out before my head explodes


You are using header("Location:inbox.php"); in the second line of your code, so the rest of it never gets executed.

Just move it to the end of the script, after you have deleted the messages you wanted to delete.


name='delete[]' value='<?php echo $messageid?>'

$_POST['delete']


What seems to be the problem is that you are adding a form tag for each checkbox (and not closing that form tag). Instead put all the checkboxes in the same form, and name them delete[] the brackets at the end put each checkbox in an array inside the $_POST array. So you can loop through them.

Here is an edited version (Note: not tested)

<?php
require "connect.php";

echo "<hr><br><div id='mail'>";

// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE 

to_user='$userfinal' ORDER BY messages_id DESC") or die(mysql_error());
$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal' 

ORDER BY messages_id DESC") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content

echo "<form name='send' method='post' action='delete.php'>";

echo '<ul>';
for($count = 1; $count <= $num_messages; $count++)
{
    $row = mysql_fetch_array($get_messages2);
    $fromuser = $row['from_user'];
    $messageid = $row['messages_id'];
    $messagetitle = $row['message_title'];
    $messageread = $row['message_read'];
    $messagedate = $row['message_date'];

    echo "
    <input type='checkbox' name='messages[]' value='$messageid'></td>
    <font face='helvetica' size='3'>$fromuser</font>&nbsp; &bull; &nbsp;
    <div align='right'>
        <a href='read_message.php?messages_id=$messageid'>$messagetitle</a>
    </div>
    <center><hr></center>
    \n\n\n";
}

echo '</form>';
?>
<input type="submit" name="Submit" value="Delete Selected">
</table>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜