简体   繁体   English

通过+1更新mysql中的自动增量列?

[英]update auto increment column in mysql by +1?

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. 用户应该能够单击“喜欢”按钮,并使其插入该配置文件的user_id并自动增加“喜欢”列。 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. 我也在尝试做到这一点,以便下次出现并喜欢相同个人资料的人也可以点击“喜欢”并每次更新“喜欢”自动增加一栏。

I am new to mysql and php and i'm really struggling with this. 我是mysql和php的新手,对此我真的很挣扎。 Could someone please show me where i'm going wrong? 有人可以告诉我我要去哪里了吗? Thanks in advance. 提前致谢。

<?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']}");

}
?>

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,所以您的Likes表架构将类似于

ID
Post_ID
Time

So next time you want to see how many likes does Post #3 has, you do 因此,下次您想查看第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. 可以在单个行中增加点赞的价值,但是效率不高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM