如果用户已经喜欢某事,则显示消息

时间:2013-01-24 20:09:44

标签: php mysql

在我的网络应用程序中,我有2个按钮:一个是“喜欢”,另一个是“不喜欢”。

用户不能喜欢自己的个人资料。

我现在正在寻找一些代码,以便如果数据库检查显示已经有一行想要喜欢或不喜欢该配置文件,则会出现一条消息:“您已经喜欢此用户”或“你已经不喜欢这个用户了“

<? if ($_POST['like']){
  if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    echo "You cannot Like yourself!<br /><br/>";
  } else {
     mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
         `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
         '{$fetchAccount['UserName']}', 'Liked', '$time')");
    echo "You Liked {$fetchUser['UserName']}!<br /><br />";
  }
}

if ($_POST['Dislike']){
  if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    echo "You cannot DisLike yourself!<br /><br />";
  } else {
    mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
      `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
      '{$fetchAccount['UserName']}', 'Disliked', '$time')"); 
    echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
  }
}
?>

有人可以帮忙吗?提前致谢。 :)

1 个答案:

答案 0 :(得分:0)

你走了。

<?php

//-------Like section---------
if ($_POST['like']){

  if(strtolower($view) == strtolower($fetchAccount['UserName'])){

    echo "You cannot Like yourself!<br /><br/>";

  } else {

     //Check whether user 'liked' the other user already.
     $selectQuery = "select * from `ProfileLikes` where `Profile` = '" . 
          $fetchUser['UserName'] ."' AND `Rated` = '" . 
          $fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Liked' ";
     $resultSetCheck = mysql_query($selectQuery);
     $rowLikedExistsArray = mysql_fetch_assoc($resultSetCheck);

     if (!empty($rowLikedExistsArray)) {

         //He 'liked' it already!
         echo "You have already liked this user! <br /><br/>";

     } else {         

         //insert new 'like'
         mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
             `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
             '{$fetchAccount['UserName']}', 'Liked', '$time')");
        echo "You Liked {$fetchUser['UserName']}!<br /><br />";

     }
  }
}

//-------Dislike section---------
if ($_POST['Dislike']){

  if(strtolower($view) == strtolower($fetchAccount['UserName'])){

    echo "You cannot DisLike yourself!<br /><br />";

  } else {

     //Check whether user 'disliked' the other user already.
     $selectQuery = "select * from `ProfileLikes` where `Profile` = '" . 
          $fetchUser['UserName'] ."' AND `Rated` = '" . 
          $fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Disliked' ";
     $resultSetCheck = mysql_query($selectQuery);
     $rowDislikedExistsArray = mysql_fetch_assoc($resultSetCheck);

    if (!empty($rowDislikedExistsArray)) {

         //He 'disliked' it already!
         echo "You have already disliked this user! <br /><br/>";

    } else {
        //insert new dislike
        mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
          `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
          '{$fetchAccount['UserName']}', 'Disliked', '$time')"); 
        echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
    }

  }
}

?>

注意:我可以看到一些优化。但是不想破坏你的代码流。