在按顺序显示消息的消息系统中,仅删除选定的消息/消息

时间:2013-06-17 19:53:39

标签: php html mysql

我在php中创建了一个消息系统。我的数据库包含以下表格:

users('user_id', 'name')
message_group('user_one', 'user_two', 'hash', 'date1', 'del1', 'del2')
messages('message_id', 'group_hash', 'from_id', 'message', 'date', 'read')

我创建了以下代码,它使用while语句从数据库中提取所有消息。 然后通过使用表单我试图删除每条消息。问题是所有邮件都被删除。如何一次只删除我想要的每一个?考虑到为了删除我放在表messages_group del1 = 1或del2 = 1的消息,分别为user_one = $ session_user_id或user_two = $ session_user_id

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="delete_all" value="Delete All" style="margin-left:250px;"/>
</form> 

这里启动php代码

  $get_con = mysql_query(" SELECT `hash`, `user_one`, `user_two`, `date1` FROM `message_group` WHERE `user_one`='$session_user_id' OR `user_two`='$session_user_id' ORDER BY `date1` DESC ");       

//start while       
while($run_con = mysql_fetch_array($get_con)){          

$hash = $run_con['hash'];
$user_one = $run_con['user_one'];
$user_two = $run_con['user_two'];

      if($user_one == $session_user_id){
        $select_id = $user_two;
      }else{
        $select_id = $user_one;
      }

 $user_get = mysql_query(" SELECT `user_id`, `name` FROM `users` WHERE `user_id`='$select_id' ");
 $run_user = mysql_fetch_array($user_get);

 $user_id = $run_user['user_id'];


$a = mysql_query(" SELECT `user_one`, `user_two`, `hash`, `date1`, `del1`, `del2` FROM `message_group` WHERE `user_one`='$session_user_id' AND `hash`='$hash' AND `del1`='1'    ");
$b = mysql_query(" SELECT `user_one`, `user_two`, `hash`, `date1`, `del1`, `del2` FROM `message_group` WHERE `user_two`='$session_user_id' AND `hash`='$hash' AND `del2`='1'    ");

if(mysql_num_rows($a)==0 && mysql_num_rows($b)==0){

echo" $select_name </br> ";
$message_query = mysql_query(" SELECT `messages_id`, `from_id`, `message`, `date` FROM messages WHERE group_hash='$hash' ORDER BY `date` ASC ");

while($run_message = mysql_fetch_array($message_query)){
$message = $run_message['message'];
}

$ka = mysql_query("SELECT `messages_id`, `group_hash`, `from_id`, `message`, `date`, `read` FROM `messages` WHERE `group_hash`='$hash' AND `read`='1'  ");


if(mysql_num_rows($ka) != 0){   
echo " <a href='messages.php?hash=$hash'>$message ...</a>"; 
}



 if(isset($_POST['delete_all'])) {                      
 if($user_one == $session_user_id){ 
   mysql_query(" UPDATE `message_group` SET `del1`='1' WHERE `hash`='$hash' ");
 }else if($user_two == $session_user_id){ 
   mysql_query(" UPDATE `message_group` SET `del2`='1' WHERE `hash`='$hash' ");
 }  
   }
}//end while

0 个答案:

没有答案