1

在我的 Web 应用程序中,我有 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 />";
  }
}
?>

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

4

1 回答 1

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 />";
    }

  }
}

?>

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

于 2013-01-25T14:31:55.353 回答