<?php
  
$data = DB::GET_STRING_ALL("SELECT * FROM `MAIL_MESSAGE` WHERE (`USER_ID` = '".$user['ID']."' OR `MY_ID` = '".$user['ID']."') AND `READ` = '0' AND `USER` = '".$user['ID']."' AND `UPDATE` = '0' ORDER BY `TIME` DESC");

WHILE ($list = $data->FETCH()){
  
  IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `MAIL` WHERE `USER_ID` = '".$list['USER_ID']."' AND `MY_ID` = '".$list['MY_ID']."' LIMIT 1") == 0){
    
    DB::GET_ADD("INSERT INTO `MAIL` (`USER_ID`, `MY_ID`, `TIME`) VALUES ('".$list['USER_ID']."', '".$list['MY_ID']."', '".$list['TIME']."')");
    
  }ELSE{
    
    DB::GET_SET("UPDATE `MAIL` SET `TIME` = '".$list['TIME']."' WHERE `USER_ID` = '".$list['USER_ID']."' AND `MY_ID` = '".$list['MY_ID']."' LIMIT 1");
    
  }
  
  IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `MAIL` WHERE `USER_ID` = '".$list['MY_ID']."' AND `MY_ID` = '".$list['USER_ID']."' LIMIT 1") == 0){
    
    DB::GET_ADD("INSERT INTO `MAIL` (`USER_ID`, `MY_ID`, `TIME`) VALUES ('".$list['MY_ID']."', '".$list['USER_ID']."', '".$list['TIME']."')");
    
  }ELSE{
    
    DB::GET_SET("UPDATE `MAIL` SET `TIME` = '".$list['TIME']."' WHERE `USER_ID` = '".$list['MY_ID']."' AND `MY_ID` = '".$list['USER_ID']."' LIMIT 1");
    
  }
  
  DB::GET_SET("UPDATE `MAIL_MESSAGE` SET `UPDATE` = '1' WHERE `UPDATE` = '0' AND `ID` = '".$list['ID']."' LIMIT 1");
  
}

IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `MAIL` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$user['ID']."' LIMIT 1") == 1){
  
  DB::GET_SET("DELETE FROM `MAIL` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$user['ID']."'");
  DB::GET_SET("DELETE FROM `MAIL_MESSAGE` WHERE `USER_ID` = '".$user['ID']."' AND `MY_ID` = '".$user['ID']."'");
  
}
  
?>