php-使用户能够收藏帖子 [英] php - Enabling users to favorite posts

查看:99
本文介绍了php-使用户能够收藏帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我希望允许用户收藏喜欢的帖子.登录的用户被定向到显示所有帖子的页面,在每个帖子下我都放置了一个指向收藏夹的超链接.我希望文本从收藏夹"更改为收藏夹",反之亦然.我该怎么做?

On my website I want to allow users to favorite posts. A logged in user is directed to a page which shows all the posts and under each one I placed a hyperlink to favorite. I want the text to change from favorite to favorited and the other way around. How do I do that?

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

推荐答案

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

Also, please note that in PHP mysql_* functions have been deprecated and are unsafe to use (they allow for SQL Injection attacks). Learn more about PDO here: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

这篇关于php-使用户能够收藏帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆