[英]how to insert data in 2 mysql table at a time with a foreign key
I have 2 mysql tables tbl_account
and tbl_user
. 我有2个mysql表tbl_account
和tbl_user
。 I want to add data into both tables using one form. 我想使用一种形式将数据添加到两个表中。 I want to insert the following, 我要插入以下内容,
tbl_account
= account_id
, account_name
, secret_key
tbl_account
= account_id
, account_name
, secret_key
tbl_user
= user_id
, account_id
, user_name
tbl_user
= user_id
, account_id
, user_name
What i'm doing is, inserting data in tbl_account
then fetching account_id
from that table and then inserting that same account_id
in tbl_user
. 我正在做的是,在tbl_account
插入数据,然后从该表中获取account_id
,然后在tbl_user
插入相同的account_id
。 I'm fetching account_id
through a secret_key
which I pass in the form. 我正在通过secret_key
中传递的secret_key
获取account_id
。 Is this the right way or there is any simple way to do this. 这是正确的方法还是有任何简单的方法可以做到这一点。 Please advice 请指教
In my opinion it would be easier to just put it in the 2 tables rather than inserting it in the first, reading it out, and then inserting again. 我认为,将其放在2个表中比将其插入第一个表,读出并再次插入要容易得多。
So i would do something like this: 所以我会做这样的事情:
$con = mysqli_connect("host","username","password","database");
mysqli_query($con,"INSERT INTO `tbl_account` (account_id, account_name) VALUES ('$account_id', '$account_name';");
mysqli_query($con,"INSERT INTO `tbl_user` (user_id , account_id, user_name) VALUES ('user_id', '$account_id', '$account_name';");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.