我有一个用于删除邮件的收件箱代码。
如果我选择一条消息,则删除所有消息。
我该如何解决这个问题?
这是我的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.";
以下是inbox.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"
然后,对于收件箱邮件,它与发件箱相同,但是以收件箱形式。
我该如何解决这个问题?
答案 0 :(得分:1)
而不是直接评估由来自POST的数据填充的变量,GET全局变量,首先使用isset()函数检查天气是否有任何值,然后可以给出此isset函数的返回值如果进行评估。
eg : $inboxbtn = $_POST['deleteinbox'];
$outboxbtn = $_POST['deleteoutbox'];
if(isset($inboxbtn)){
$selectall = $_POST['selectall'];
if(isset($selectall)){
}
}
我认为你有问题,请检查一下...... http://in2.php.net/isset https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
答案 1 :(得分:0)
我认为你更新要删除的邮件的逻辑是错误的。
$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'");
}
}
在上面的代码中,您正在检查变量&#34; $ value&#34;在if()条件下,每个循环的这个变量都会得到一些值
$msg_id = $row['id'];
$value = "cb"."$msg_id";
并传递if,因为while循环将循环遍历属于特定用户的每条记录,所有消息都将被更新以获得DELETE。
if ($value){
mysql_query("UPDATE `messages` SET `to_delete`='1' WHERE `to_user`='$user' AND `id`='$msg_id'");
}
查看你的逻辑......
答案 2 :(得分:0)
在下面的delete_message.php中,一个是你的代码..
$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'");
}
相应地更改代码,如下所示....
$value = "cb"."$msg_id";
if (isset($_POST['$value'])){
mysql_query("UPDATE `messages` SET `to_delete`='1' WHERE `to_user`='$user' AND `id`='$msg_id'");
}