创建一个类的函数PHP MySQL的AJAX [英] Creating a Like function php mysql ajax

查看:107
本文介绍了创建一个类的函数PHP MySQL的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在寻找和发现非常关注这个问题上, 但没有什么令人满意的。

Alright, I have been searching and finding very much about this subject, but nothing satisfying.

我想跟踪谁已经喜欢什么,不只是增加一个+1到表。

I want to keep track of who has liked what, not just add a +1 to a table.

我有三个表:文章,评论和喜欢

I have three tables: posts, comments and likes.

表的设计看起来像这样的时刻

The table design looks like this for the moment

CREATE TABLE IF NOT EXISTS `likes` (
  `like_id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `post_id_fk` INT(11),
  `comment_id_fk` INT(11),
  `uid_fk` int(11) NOT NULL,
  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
FOREIGN KEY (uid_fk) REFERENCES users(uid),
FOREIGN KEY (post_id_fk) REFERENCES post(post_id),
FOREIGN KEY (comment_id_fk) REFERENCES comments(comments_id),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

正如你可以看到我有三个不同的外键,uid_fk用户的uid这样我就可以知道是谁喜欢什么。现在来出现的问题,一个外键的post_id,一个用于comments_id。

And as you can see i have three different foreign keys, uid_fk for the user uid so i can know who has liked what. And now comes there problem, one foreign key to post_id and one for comments_id.

MySQL不会接受一个外键,如果它不存在。如果我想像的注释,它不会让因为post_id_fk的外键。

Mysql won't accept a foreign key if it doesn't exist. if i want to "like" a comment, it won't let be because of the foreign key of post_id_fk.

如何解决这个数据库烂摊子?

How to solve this DB mess?

和AJAX的喜欢/ delike问题:

And the AJAX like/delike problem:

我发现这个<一href="http://stackoverflow.com/questions/13469947/jquery-changing-class-of-button-with-ajax-call">jQuery :更改类的按钮使用AJAX调用当我正在寻找,它看起来simpel,非常漂亮。 ,我也遵循这一 http://pluscss.com/tutorials / AJAX样脚本使用-PHP-MySQL的,jQuery的教程。但我有问题,将它们结合起来。

I found this jQuery : Changing class of button with AJAX call when i was searching, and it looks simpel and very nice. And i have also follow this http://pluscss.com/tutorials/ajax-like-script-using-php-mysql-jquery tutorial. But I'm having problems combine them.

这是我想要做的:

  1. 算喜欢目前的金额
  2. 检查,如果用户之前都喜欢它
  3. 给用户喜欢的选项(或delike如果pviously喜欢$ P $)

使用Ajax和like.php

with ajax and like.php

可能有人能帮我解决这个我会非常感激!

Could someone help me with this i would be very thankful!

推荐答案

这将是更好的分离表。 创建post_likes表和comments_likes表。 这样,不仅你摆脱你的存在的问题,但结构更加解耦和可重用性。

It would be better to separate the tables. Create a post_likes table and comments_likes table. That way not only you're getting rid of your existing problem but the structure is more decoupled and more reusable.

这篇关于创建一个类的函数PHP MySQL的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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