简体   繁体   English

在PHP / MySQL中更新关系数据库

[英]Updating relational database in PHP/MySQL

I am teaching myself about web programming. 我正在自学网络编程。 I am trying to create a user account for a website. 我正在尝试为网站创建用户帐户。 I have two databases - first one (db_users - userid (auto increment), username, password) to store user information like the username, password, etc. and second one (db_address - userid (auto increment), address1, address2, city) to store the user's address. 我有两个数据库-第一个数据库(db_users-用户名(自动递增),用户名,密码),用于存储用户名,密码等用户信息,第二个数据库(db_address-用户名(自动递增),地址1,地址2,城市)存储用户的地址。

Now when I register a user, how do I ensure that both the tables are updated with the correct user id? 现在,当我注册用户时,如何确保使用正确的用户ID更新两个表? Or is that not how a relational database works? 还是这不是关系数据库的工作方式?

A user data is not the case for the relational database. 关系数据库不是用户数据。
There is no point in having 2 tables for the user (as well as having 2 fields for the address). 为用户提供2个表(以及为地址提供2个字段)毫无意义。

Make it one table, then add comments table and have user id field in it. 使它成为一个表,然后添加注释表并在其中包含用户ID字段。
Thus you will have a real case of the relational database. 因此,您将拥有关系数据库的真实案例。

Without going into code you want to do: 无需编写代码即可:

  1. Insert details into user table 将详细信息插入用户表
  2. Get ID inserted using $id = mysql_insert_id() 使用$id = mysql_insert_id()获取插入的$id = mysql_insert_id()
  3. Insert details into address table using $id 使用$id详细信息插入地址表

EDIT: If using more than one MySQL connection you need to do $id = mysql_insert_id($connection) 编辑:如果使用多个MySQL连接,则需要执行$id = mysql_insert_id($connection)

First of all, I think you probably have two tables, not two databases. 首先,我认为您可能有两个表,而不是两个数据库。 Second, your address table should have an addressid column, which you should auto-increment. 其次,您的地址表应具有一个addressid列,您应将其自动递增。 You are responsible yourself for setting the proper userid in the address table. 您有责任在地址表中设置正确的用户标识。 This way you can have multiple addresses per account. 这样,每个帐户可以有多个地址。 For example, 1 for delivery and 1 for billing. 例如,1表示交货,1表示计费。

Btw, if you only plan to have 1 address per user you could consider merging the two tables and adding the address information to the account table. 顺便说一句,如果您仅计划每个用户拥有1个地址,则可以考虑合并两个表并将地址信息添加到帐户表中。 Formally, this would go against normalization rules, but in this case it would probably be more practical. 形式上,这将违反规范化规则,但是在这种情况下,它可能更实用。

Userid should not be the auto incremented index in the second table. 用户ID不应是第二个表中的自动递增索引。 Separating user and address data means a user could be connected to more than 1 address, and multiple users could have same address (living together, in other words). 将用户和地址数据分开意味着一个用户可以连接到多个地址,并且多个用户可以具有相同的地址(换句话说,在一起生活)。 Or you could merge the tables, if it suits you. 或者,如果合适的话,您可以合并表格。

db_users: db_users:

  • userid (auto increment) 用户ID(自动递增)
  • username 用户名
  • password 密码

db_address: db_address:

  • address_id (auto increment) address_id(自动递增)
  • userid 用户身份
  • address1 地址1
  • address2 地址2
  • city

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

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