我究竟做错了什么?邮件不会删除

时间:2011-06-20 23:20:20

标签: php sql sql-delete

我一直在处理我的delete.php文件,删除所选邮件几个小时就没有任何进展。如何使用我设置的代码删除邮件?

这是我现在如何设置delete.php的代码

<?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'");

    ?> 

这是我整个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; &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>

在我爆炸之前请帮忙

3 个答案:

答案 0 :(得分:0)

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

$_POST['delete']

答案 1 :(得分:0)

您在代码的第二行使用header("Location:inbox.php");,因此其余内容永远不会被执行。

删除要删除的邮件后,将其移至脚本末尾。

答案 2 :(得分:0)

似乎问题在于您是为每个复选框添加表单标记(而不是关闭该表单标记)。而是将所有复选框放在相同的表单中,并将它们命名为delete[]最后的括号将每个复选框放在$ _POST数组中的数组中。所以你可以循环使用它们。

这是一个经过编辑的版本(注:未经测试)

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