简体   繁体   English

如何使用php和mysql将外键字段链接到另一个表的主键字段?

[英]How to link the foreign key field to a primary key field from another table using php and mysql?

I got 2 tables for example a USER TABLE with id as primary key(auto-increment) and a BOOKING TABLE with userid as foreign key (integer) of id in user table. 我有2个表,例如,一个以id为主键(自动递增)的USER TABLE和一个以userid为id的外键(整数)的BOOKING TABLE。 Using phpmyadmin mysql database. 使用phpmyadmin mysql数据库。

How do i write a php script where i can link this two fields together so that they are always related? 我如何编写一个php脚本,在其中可以将这两个字段链接在一起,以便始终保持关联? So when the user enters name and books a tour on the web form, it does everything automatically on the backend. 因此,当用户输入名称并在Web表单上预订游览时,它将自动在后端执行所有操作。

I am new to php and mysql and searched so many forums and watch tutorials but still dont have a clue. 我是php和mysql的新手,并且搜索了很多论坛和观看教程,但仍然没有任何线索。 Please help! 请帮忙!

What you are currently looking is how to join multiple tables. 您当前正在寻找的是如何联接多个表。 The query below uses an INNER JOIN which displays only records which has matches on both tables. 下面的查询使用一个INNER JOIN ,它仅显示在两个表上都匹配的记录。

SELECT  a.*, b.*
FROM    User a
        INNER JOIN Booking b
            ON a.ID = b.userID
// WHERE    ..conditions here..

To further gain more knowledge about joins, kindly visit the link below: 要进一步获得有关联接的知识,请访问以下链接:

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

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