简体   繁体   English

如何使用提交按钮将用户标识添加到另一个表

[英]How to add user id to another table with a submit button

I am creating a site that allows users to view desired 'teams' and can then join them with the click of one button. 我正在创建一个站点,允许用户查看所需的“团队”,然后单击一个按钮即可加入他们。

I have my users table which contains: user_id, user_name, team_id 我的用户表包含: user_id,user_name,team_id

Then, I have my teams table which contains: team_id, team_name, team_players 然后,我有一个teams表,其中包含: team_id,team_name,team_players

How would I go about having the users to join a group, each user can also only be in 1 team at a time. 我将如何让用户加入群组,每个用户一次只能加入一个团队。

You should start by adding the team_id field to the users table as a foreign key and allow it to be NULL. 您应该首先将team_id字段作为外键添加到users表中,并使其为NULL。

Then you would display the team names in an html form with a radio button for each team. 然后,您将以html格式显示团队名称,并为每个团队单选按钮。

In a PHP file (which should be set to the action of your form) create an if statement based on the values you assigned to each radio button. 在一个PHP文件(应将其设置为表单操作)中,根据分配给每个单选按钮的值创建一个if语句。 In each if block, execute a sql UPDATE statement that will add the appropriate group_ID to the right user instance. 在每个if块中,执行一条sql UPDATE语句,它将适当的group_ID添加到正确的用户实例。

If you want each user to be able to join multiple teams, and each team to have multiple users, then you need a "join table." 如果您希望每个用户都能够加入多个团队,并且每个团队都拥有多个用户,那么您需要一个“加入表”。

Table teams_users would contain team_id , user_id . team_users将包含team_iduser_id You can make a composite primary key on team_id, user_id (preventing a user from joining the same team twice). 您可以在team_id和user_id上创建复合主键(防止用户两次加入同一团队)。

Then you can get a team with: 然后,您可以拥有一个团队:

SELECT * FROM users t1 right join teams_users t2 ON t1.team_id = t2.team_id WHERE t2.team_name = 'the rascals'

Even if you only want players to join one team at a time, you might still want to use the join table in case you ever change your mind. 即使您只希望一次只加入一个团队,您仍然可能想要使用加入表,以防万一您改变主意。 It would be very easy. 这将非常容易。 To only allow one team per user, put a unique constraint on user_id in the join table. 要仅允许每个用户一个团队,请在联接表中对user_id设置唯一约束。 If you later decide you want to allow multiple teams, you just remove that constraint. 如果您以后决定要允许多个团队,则只需删除该约束即可。

If a user tries the "join team" action, you simply check for the user_id's existence in the join table. 如果用户尝试执行“加入团队”操作,则只需检查加入表中user_id的存在。

SELECT * FROM teams_users WHERE user_id = $user_id

If it does exist, you retrieve its matching team_id and tell them, "sorry, you are already in team 'the rascals'. You must leave that team if you want to join another." 如果确实存在,则检索其匹配的team_id并告诉他们:“抱歉,您已经在团队'rscals'中。如果您想加入另一个团队,则必须离开该团队。” If they drop their team, you simply do: 如果他们放弃了团队,您只需:

DELETE from teams_users WHERE user_id = 5

If they add a team, you just do: 如果他们添加了团队,则只需执行以下操作:

INSERT INTO teams_users ($team_id, $user_id) #// (assuming PHP variables).

The INSERT query will only work if they are not already in a team. INSERT查询仅在它们尚未加入团队时才起作用。 If they are you would get an error message. 如果它们是您将收到错误消息。 You could also look at "INSERT ON DUPLICATE KEY UPDATE ..." queries. 您还可以查看“在重复键更新上插入...”查询。 But I would advise against that because you want to warn users before they change teams. 但我建议您这样做,因为您想在用户更换团队之前警告用户。

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

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