简体   繁体   English

是否可以使用 select 查询从两个表中获取 PK 并将这些 PK 的值插入另一个将它们作为 FK 的表中?

[英]Is it possibly to use a select query that gets the PK from two tables and inserts the values of those PK into another table that has them as FK?

Hi as my question states I was wondering if it possible to get the a PK from two tables and then insert them into another table where they are already set in as FK.嗨,正如我的问题所述,我想知道是否可以从两个表中获取一个 PK,然后将它们插入到另一个表中,在那里它们已经设置为 FK。 I currently have a form that also inserts data that the user inputs into that same table.我目前有一个表单,它还可以将用户输入的数据插入到同一个表中。 For example I want, the user to choose the rooID they want from drop down menu, input checkindate, checkoutdate, contactnumber, and booking extras.例如我想要,用户从下拉菜单中选择他们想要的 rooID,输入 checkindate、checkoutdate、contactnumber 和预订附加信息。 This then gets put into my booking table along with the two FK.然后将其与两个 FK 一起放入我的预订表中。 Below is my booking table columns to help understand the inputs.下面是我的预订表列,以帮助理解输入。

  • bookingID (PK)预订ID(PK)
  • checkindate登记日期
  • checkoutdate离开日期
  • contactnumber联系电话
  • bookingextras预订附加服务
  • customerID (FK) from customer table客户表中的客户 ID (FK)
  • roomID (FK) from room table房间表中的 roomID (FK)

bookproccess.php: bookproccess.php:

<?php

include_once 'config.php';
$conn = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);
if(isset($_POST['submit']))
{    
 $roomID = $_POST['rooID'];
 $checkindate = $_POST['checkindate'];
 $checkoutdate = $_POST['checkoutdate'];
 $contactnumber = $_POST['contactnumber'];
 $bookingextras = $_POST['bookingextras'];

     
 $query = "SELECT booking.bookingID, customer.customerID, room.roomID FROM ((booking
 INNER JOIN customer ON booking.customerID = customer.customerID)
 INNER JOIN room ON booking.roomID = room.roomID) WHERE roomID = ".$roomID;

 $result = $conn->query( $query );
 if( $result ) {
 
 $sql = "INSERT INTO booking (roomID, checkindate, checkoutdate, contactnumber, bookingextras, customerID)
 VALUES ('$roomID''$checkindate','$checkoutdate','$contactnumber','$bookingextras','$customerID')";
 if (mysqli_query($conn, $sql)) {
    echo "New record created successfully !";
 } else {
    echo "Error: " . $sql . "
" . mysqli_error($conn);
 }
 mysqli_close($conn);
}}
?>
 
 

makeabooking.php: makeabooking.php:

<h1>Booking</h1>
<h2><a href='menu.php'>[Return to the main page]</a></h2>

<form method = "post" action = "bookproccess.php">
<p>
<label for = "rooID">Room: (name, type, beds): </label>
<select id = "rooID" name = "rooID" required>
<option name = "" value = "" disabled selected>Select</option>
<option name = "1" value = "1">Kellie, S, 5</option>
<option name = "2" value = "2">Herman, D, 2</option>
<option name = "3" value = "3">Scarlett, D, 2</option>
<option name = "4" value = "4">Jelani, S, 5</option>
<option name = "5" value = "5">Sonya, S, 4</option>
<option name = "6" value = "6">Miranda, S, 2</option>
<option name = "7" value = "7">Helen, S, 2</option>
<option name = "8" value = "8">Octavia, D, 3</option>
<option name = "9" value = "9">Bernard, D, 5</option>
<option name = "10" value = "10">Dacey, D, 1</option>
</select>
</p> 

<p>
<label for="checkindate">Check in date: </label>
<input type="date" name="checkindate"required> 
</p>  
<p>
<label for="checkout">Check out date: </label>
<input type="date" name="checkoutdate"required> 
</p>  
<p>  
<label for="contactnumber">Contact number: </label>
<input type="text" name="contactnumber" required> 
</p>
<p>
<label for="bookingextras">Booking extras: </label>
<input type="text" name="bookingextras" size="100" minlength="5" maxlength="200"  required> 
</p> 

<input type="submit" name="submit" value="submit">
<a href="menu.php">[Cancel]</a>

 </form>
 </body>
 </html>

Of course it is possible because that's what you should do.当然这是可能的,因为那是你应该做的。

It seems you have a many to many relationship between Customers and Rooms which is resolved with an associative table Bookings.似乎客户和房间之间存在多对多关系,这可以通过关联表 Bookings 解决。 Bookings has 2 foreign keys - one to Customers and one to Rooms. Bookings 有 2 个外键 - 一个给客户,一个给房间。

Typically what you want is to be on a customer master detail page which gives you access to the customer id - the detail being the booking.通常,您想要的是位于客户主详细信息页面上,该页面可让您访问客户 ID - 详细信息是预订。

To add a booking you introduce a row which is already aware of the master customer id and then in the booking context, you introduce a fixed list of rooms in some fashion (which come with room ids).要添加预订,您需要引入一个已经知道主客户 ID 的行,然后在预订上下文中,您以某种方式引入一个固定的房间列表(带有房间 ID)。 This will depend on if a customer (a party/family) can occupy one room or multiple rooms.这将取决于客户(一方/家庭)是否可以占用一个房间或多个房间。

By the time you submit the booking to be saved in the database, it has CustomerID, Booking ID and any other booking level details you need.当您提交要保存在数据库中的预订时,它已包含 CustomerID、Booking ID 和您需要的任何其他预订级别详细信息。

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

相关问题 PK和FK表设计之间的混淆 - Confusion between PK and FK table design Laravel 4.2和AngularJS将数据插入到多个表中,其中Table_1中的PK为Table_2和Table_3中的FK - Laravel 4.2 & AngularJS Insert data into multiple tables where PK in Table_1 is FK in Table_2 & Table_3 如何在MySQL表中合并两个冗余记录,保持所有PK / FK关系? - How can I merge two redundant records in a MySQL table, maintaining all PK/FK relationships? Lumen/Laravel - Eloquent:使用 BIGINT 无符号作为 PK 和 FK 的表的迁移 - Lumen/Laravel - Eloquent: Migration of tables with BIGINT unsigned as PK and FK 使用PHP-MySQL从PK更新FK - Update FK from PK using PHP-MySQL 与来自同一 PK Laravel 8 的 2 个 FK 引用的多对多关系 - Many to Many Relationship with 2 FK referencing from same PK Laravel 8 从具有多个要求的两个表中选择值,但仅使用其中一个 - Select values from two tables with multiple requirements but only use one of them 使用FK作为PK + Symfony2和Doctrine删除行 - Remove row with FK as PK + Symfony2 and Doctrine PHP将值插入两个表并将这些表的键插入链接表 - PHP Insert values into two tables and insert keys from those tables into link table 如何将表1中的PK(主键)插入表2中的FK(外键)然后显示结果 - How To Insert PK (Primary Key) in Table1 To FK(Foreign Key) in Table2 then show it the result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM