简体   繁体   English

如何从具有主键自动增量的表中向具有外键的表中插入数据?

[英]How can I insert data to a table which has a foreign key, from a table which has a primary key auto-increment?

I have 4 mySQL tables with the following entries: 我有4个带有以下条目的mySQL表:

user
-user_id PK,AI
-user_name
-user_mobil
-user_passw
-user_email

bookingdetails
-booking_id PK,AI
-booking_date
-booking_time
-person_number

booking
-booking-_id FK
-restaurant_id CK
-user_id CK

restaurant
-restaurant_id PK
-restaurant_name
-restaurant_address
-restaurant_description

I would like to make a booking, I insert all the bookingdetails data, which gives me a AI booking_id, and after I would like to make my booking table and insert the restaurant_id and the user_id With the same booking_id which was given by the bookingdetails table. 我想进行预订,我插入所有的bookingdetails数据,这给了我一个AIbooking_id,然后我要创建我的预订表,然后插入restaurantdetails表给出的相同的booking_id的restaurant_id和user_id 。

I made the following code for achieve that in php on a localserver: 我在本地服务器上的php中实现了以下代码:

 $booking_date=$_POST["booking_date"];
 $booking_time=$_POST["booking_time"];
 $number_of_place=$_POST["number_of_place"];
 $customer_id=$_POST["customer_id"];
 $restaurant_id=$_POST["restaurant_id"];
 $res;


 $sql_query = "INSERT INTO bookingdetails(booking_date, booking_time, number_of_place) VALUES ('$booking_date','$booking_time', '$number_of_place')";
 $sql_query2 = "INSERT INTO `booking`(`booking_id`, `customer_id`, `restaurant_id`) SELECT booking_id, '$customer_id', '$restaurant_id' FROM   bookingdetails ORDER BY booking_id DESC LIMIT 1 ;";


 if(mysqli_query($con,$sql_query))
 {


 }
 else
 {


 }



 if(mysqli_query($con,$sql_query2))
 {


 }
 else
 {


 } 



 ?>

Is that a legit solution on a server which joining to an Android app? 这是加入Android应用程序的服务器上的合法解决方案吗? Is there any case, that i don't get the good id on the second query? 有没有什么情况,我在第二个查询中没有得到好的ID? What would be a better solution? 有什么更好的解决方案?

Answer given in comment by @Mark Ng @Mark Ng在评论中给出的答案

Use last insert id, criteria is that your pk has to be AI. 使用最后一个插入ID,条件是您的pk必须是AI。

The mysqli_insert_id() function returns the id (generated with AUTO_INCREMENT) used in the last query. mysqli_insert_id()函数返回上一次查询中使用的ID(由AUTO_INCREMENT生成)。

Source: w3schools.com/php/php_mysql_insert_lastid.asp 资料来源: w3schools.com/php/php_mysql_insert_lastid.asp

To elaborate 详细说明

you have to execute the query from which you need the last inserted id, then you can access that by using 您必须执行查询,从该查询中您需要最后一个插入的ID,然后可以通过使用以下命令进行访问

 $last_id = $conn->insert_id;

which in turn you can use for your following query. 反过来,您可以将其用于以下查询。

Note: 注意:

I see you use a query to use the results for your insert query, but your syntax is incorrect (your missing values ) 我看到您使用查询将结果用于插入查询,但是语法不正确(您缺少的values

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

相关问题 将数据插入具有外键字段的php表中 - Insert Data into a table in php which has a foreign key field 如何获取表的自动增量主键 - how to get the auto-increment primary key of a table 如何在PHP Script中插入已将前例键引用到另一个表的主键的记录? - How to insert record which has foregin key referenced to primary key of another table in PHP Script? PHP mySQL - 在主键上使用自动递增将新记录插入表中 - PHP mySQL - Insert new record into table with auto-increment on primary key 如何获取主键,这是在PHP中输入的行的自动增量? - How to get the primary key which is an auto-increment for the row just entered in PHP? 如何插入到包含外键的表中 - How to insert into table which contain foreign key 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 如何将行自动添加到表中,这会影响主键 - How Can i add row to my table that is auto increment that will affect my primary key 如何使用自定义自动增量主键 Laravel 添加 model - How to add a model with custom auto-increment primary key Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM