如何将数据发送到具有与主键相同的用户ID的两个表? [英] How to send data to two tables with same user ID as primary key?

查看:89
本文介绍了如何将数据发送到具有与主键相同的用户ID的两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP完全陌生,我试图创建一个注册页面。要注册,用户必须创建一个用户名,密码和电子邮件,这些用户名,密码和电子邮件放在名为 users 的表中。

他们还输入地址详细信息,放在名为 customer_info 的表格中。



在这两个表中,我创建了一个自动增量,主键为'user_id'。



当表单完成时,填充并输入数据,但数据没有绑定,所以有两个user_id,一个在 users 中,一个在 customer_info 中。 / p>

首先,我创建已输入的值并将其分配给变量。然后我使用以下查询将变量放到我的表中:

  $ result = mysql_query(
INSERT INTO`用户名(用户名,密码,电子邮件)VALUES('$ value1','$ value2','$ value3')
);

 <$ $'$ value4','$ value5'$ result = mysql_query ','$ value6','$ value7','$ value8','$ value9','$ value10','$ value11','$ value12','$ value13')
);

如何设置它,以便它只为这两个表创建一个用户标识数据集)?



在我的查询中是否存在缺少应该连接表的内容?

解决方案

在任何之前,您不应再使用mysql_ *扩展名。转向 PDO mysqli






您的技术会生成两个不同的唯一ID。关键是只有 1 ,以便它可以是唯一的,并链接关于该唯一ID的信息。



users表是一个具有唯一标识 user_id ,这是您的auto_increment列。 customer_info表还可以包含 info_id 唯一列,但必须包含 user_id 列,其中包含用户的 user_id ,将行连接在一起。



添加外键添加到您的表中,这样数据的完整性不会受到影响。



这样查询后:

  $ result = mysql_query(
INSERT INTO`users`(用户名,密码,电子邮件)VALUES('$ value1','$ value2','$ value3')
);

获取插入ID:

  $ id = mysql_insert_id(); 

然后运行您的其他查询:



< (p_add_pc,p_add_pc,p_add_num,p_add_road,p_add_town,p_add_pc)插入'customer_info`(user_id,firstname,lastname,b_add_num,b_add_road,b_add_town,b_add_road,b_add_town,p_add_pc)pre> $ result = mysql_query(
)VALUES ( '的​​$ id', '$值4', '$值5', '$ value6', '$ value7', '$ value8', '$ value9', '$ value10', '$ value11', '$ value12' ,'$ value13')
);


I am totally new to PHP and I'm trying to create a registration page. To register, a user has to create a username, password, email, which are put into a table called users.

They also enter address details which is put into a table called customer_info.

In both tables I have created an auto increment, primary key called 'user_id'.

When the form is completed it fills out and enters the data, but the data is not banded and so there are two user_id, one in users and one in customer_info.

First I create values (from the post) that have been entered and assign them to variables. Then I put the variables into my table using the following query:

$result = mysql_query(
    "INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);

and

$result = mysql_query(
    "INSERT INTO `customer_info`(firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);

How would I set it so that it creates only one user id for both tables (making a connection between the sets of data)?

Is there something missing in my query, that should connect the tables?

解决方案

Before anything, you should not use mysql_* extension anymore. Go towards PDO or mysqli


Your technique generates two different unique ids. The point is to have only one, so that it can be unique, and link information on that unique id.

The users table is the one with that unique id, user_id, which is your auto_increment column. The customer_info table can also have a info_id unique column, but must contain a user_id column, which will contain the user's user_id, linking the rows together.

It would also be a great moment to add a foreign key to your tables so that integrity of the data won't be compromised.

so after this query:

$result = mysql_query(
    "INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);

get the insert id:

$id = mysql_insert_id();

then run your other query with it:

$result = mysql_query(
    "INSERT INTO `customer_info`(user_id,firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$id','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);

这篇关于如何将数据发送到具有与主键相同的用户ID的两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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