通过+1更新mysql中的自动增量列? [英] update auto increment column in mysql by +1?

查看:44
本文介绍了通过+1更新mysql中的自动增量列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗,我正在尝试创建一个类似系统的用户.用户应该能够单击喜欢"按钮并让它插入该个人资料的 user_id 并自动增加喜欢"列.我也在努力让下一个喜欢同一个人资料的人也可以点击喜欢并每次将喜欢的自动递增列更新一个.

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.

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

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.

<?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.

然后您可以通过引用 ID 查询 DB 搜索.

THen you can query the DB searching by reference ID.

让你有帖子和喜欢.每个帖子都有一个 Post_ID,因此您的喜欢表架构类似于

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

ID
Post_ID
Time

所以下次你想看看帖子 #3 有多少喜欢的时候,你做

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

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更新mysql中的自动增量列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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