Can someone please help me, I am trying to create a users like system. Users should be able to click A like button and have it insert the user_id of that profile and auto increment the likes column. I'm also trying to make it so that the next person who comes along and likes the same profile, can also hit like and update the likes auto increment column by one each time.

有人可以帮助我,我正在尝试创建一个类似系统的用户。用户应该能够单击A like按钮并让它插入该配置文件的user_id并自动增加likes列。我也试图让它成为下一个出现并喜欢相同配置文件的人,也可以点击并每次更新喜欢的自动增量列。

I am new to mysql and php and i'm really struggling with this. Could someone please show me where i'm going wrong? Thanks in advance.

我是mysql和php的新手,我真的很挣扎。有人可以告诉我哪里出错了吗?提前致谢。

<?php

require_once('includes/session.php');
require_once('includes/functions.php');
require('includes/_config/connection.php');

session_start();

    confirm_logged_in();

    if (isset ($_GET['to'])) {
    $user_to_id = $_GET['to'];


}


if (!isset($_GET['to']))
    exit('No user specified.');

$user_id = $_GET['to'];


$result = mysql_query("SELECT * FROM ptb_likes WHERE liked_id ='".$user_to_id."' ");

if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE ptb_likes SET likes +1 WHERE liked_id = '".$user_to_id."' ");


    $autoinc = mysql_query("ALTER TABLE likes AUTO_INCREMENT = $id");
}
else
{
    mysql_query("INSERT INTO ptb_likes (liked_id) VALUES ('".$user_to_id."') ");
}



if($result) 
{ 

header("Location: {$_SERVER['HTTP_REFERER']}");

}
?>

1 个解决方案

#1


0

If you're doing a "Like"-wise system, it's preferable to have a table with the likes one per row.

如果你正在做一个“喜欢”方式的系统,最好有一个表格,每行一个。

THen you can query the DB searching by reference ID.

您可以通过引用ID查询数据库搜索。

Let's you have posts, and likes. Each post has a Post_ID, so your likes table schema would be something like

让我们有帖子,喜欢。每个帖子都有一个Post_ID,所以你喜欢的表模式会是这样的

ID
Post_ID
Time

So next time you want to see how many likes does Post #3 has, you do

所以下次你想看看Post#3有多少喜欢,你会这么做

SELECT COUNT(*) FROM posts WHERE post_id = 3

That way you have better control and reduce queries.

这样您就可以更好地控制和减少查询。

Incrementing the value of likes in a single row, would be done, but not efficient.

可以在单行中增加喜欢的值,但效率不高。

更多相关文章

  1. Linux下修改MySQL初始密码、开启远程登录、授权远程登录用户
  2. 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最
  3. 是一个mysql临时表,每个用户访问创建它的脚本是唯一的...?
  4. MVC框架——学生信息管理系统(多表,多事务如何处理,一个用户如何共
  5. MySQL5.7以上版本root用户空密码修改(windows系统、zip版MySQL)
  6. MySql查询脚本,每月统计活动用户。
  7. 获取喜欢和评论的帖子 - 一个查询?
  8. MySQL用户管理(5.7.20-winx64)
  9. Laravel - 如何为用户赋予多重角色?

随机推荐

  1. SQLite格式编号始终为2位小数
  2. windows下搭建Apache+Mysql+PHP开发环境
  3. 线程往数据库里插数据时偶尔会报错
  4. OSX下完全删除MySQL和怎样使MySQL支持中
  5. ORACLE 01843. 00000 - “不是有效月份
  6. unity连接数据库MySQL简单例子
  7. MySQL binlog格式解析
  8. PostgreSQL自动备份批处理脚本
  9. 一步一步学MySQL----13 多表数据记录查询
  10. 解决用户 'sa' 登录失败。原因: 未与信任