如何从具有主键自动递增的 table 向具有外键的 table 插入数据?
How can I insert data to a table which has a foreign key, from a table which has a primary key auto-increment?
我有 4 个 mySQL table 具有以下条目:
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
我想进行预订,我插入了所有预订详细信息数据,这为我提供了一个 AI booking_id,在我想进行预订后 table 并插入 restaurant_id 和 user_id 与预订详情 table 给出的相同 booking_id。
我在 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
{
}
?>
在加入 Android 应用程序的服务器上,这是一个合法的解决方案吗?有没有任何情况,我在第二次查询中没有得到好的 id?什么是更好的解决方案?
@Mark Ng 在评论中给出的答案
使用最后插入的id,条件是你的pk必须是AI。
mysqli_insert_id() 函数 returns 上次查询中使用的 id(用 AUTO_INCREMENT 生成)。
来源: w3schools.com/php/php_mysql_insert_lastid.asp
详解
您必须执行需要最后插入的 ID 的查询,然后您可以使用
访问
$last_id = $conn->insert_id;
这又可以用于您的后续查询。
注:
我看到您使用查询来使用插入查询的结果,但是您的语法不正确(您缺少 values
)
我有 4 个 mySQL table 具有以下条目:
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
我想进行预订,我插入了所有预订详细信息数据,这为我提供了一个 AI booking_id,在我想进行预订后 table 并插入 restaurant_id 和 user_id 与预订详情 table 给出的相同 booking_id。
我在 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
{
}
?>
在加入 Android 应用程序的服务器上,这是一个合法的解决方案吗?有没有任何情况,我在第二次查询中没有得到好的 id?什么是更好的解决方案?
@Mark Ng 在评论中给出的答案
使用最后插入的id,条件是你的pk必须是AI。
mysqli_insert_id() 函数 returns 上次查询中使用的 id(用 AUTO_INCREMENT 生成)。
来源: w3schools.com/php/php_mysql_insert_lastid.asp
详解
您必须执行需要最后插入的 ID 的查询,然后您可以使用
访问 $last_id = $conn->insert_id;
这又可以用于您的后续查询。
注:
我看到您使用查询来使用插入查询的结果,但是您的语法不正确(您缺少 values
)