php - 使用户能够收藏喜欢的帖子

时间:2014-03-22 20:41:45

标签: php jquery html mysql database

在我的网站上,我想让用户收藏最喜欢的帖子。登录用户被定向到显示所有帖子的页面,在每个帖子下面我放置了一个到收藏夹的超链接。我希望文本从最爱变为收藏,反之亦然。我该怎么办?

HTML和PHP

<?php
session_start();
require_once('connection.php');

mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);
?>

<a href="#" class="favourite">Favourite</a>

我的数据库中的表

CREATE TABLE `user` (
  `username` varchar(45) NOT NULL,
  `email` varchar(45) NOT NULL,
  `password` varchar(45) NOT NULL,
  `profilepic` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `post` (
  `post_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `dato` date NOT NULL,
  `category` varchar(100) NOT NULL,
  `description` varchar(500) NOT NULL,
  `text` longtext NOT NULL,
  PRIMARY KEY (`post_id`),
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

CREATE TABLE `favorite` (
  `username` varchar(45) NOT NULL,
  `post_id` int(11) NOT NULL,
  PRIMARY KEY (`username`,`post_id`),
  KEY `fk_favorite_post1_idx` (`post_id`),
  CONSTRAINT `fk_favorite_user` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_favorite_post1` FOREIGN KEY (`post_id`) REFERENCES `post` (`post_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:1)

PHP

<?php
session_start();
require_once('connection.php');

mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);

if(in_array($_POST['id'], $row_favorite))
{
   //is already favourited, run a query to remove that row from the db, so it won't be favorited anymore

}
else
{
   //post is not favourited already, run a query to add a new favourite to the db.
}

?>

HTML

<a href="#" class="favourite" data-id="<?php echo $post_id; ?>">Favourite</a>

的jQuery

$(document).ready(function() {
    $('.favourite').on('click', null, function() {
        var _this = $(this);
        var post_id = _this.data('id');
        $.ajax({
          type     : 'POST',
          url      : '/file.php',
          dataType : 'json',
          data     : 'id='+ post_id,
          complete : function(data) {
               if(_this.siblings('.typcn-star-outline'))
               {
                 _this.html('<span class="typcn typcn-star-full-outline"></span>Favourite');
               }
               else
               {
                 _this.html('<span class="typcn typcn-star-outline"></span>Favourited');
               }
            }
        });
    });
});

另外,请注意,在PHP中,mysql_ *函数已被弃用且使用不安全(它们允许SQL注入攻击)。在此处了解有关PDO的更多信息:http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059