将第一个表的自动增量id同时作为另一个表的外部id


To get tha auto increment id of first table as the foreign id of another table at a same time

我为一个表单创建了两个不同的表。表单的上部插入到一个表中,表单的下部插入到另一个表中。我必须将第一个表的主Id作为另一个表的外Id

有可能吗?又是怎样呢?下面是php

的查询代码
    if(isset($_POST['add_hostel']))
{
    extract($_POST);
    $checkbox = implode(',',$_POST['checkbox']);
    $checkbox1 = implode(',',$_POST['checkbox1']);
    $insert_details = mysql_query("INSERT INTO `add_hostel` (owner_id,accomodation_type,hostel_for,hostel_name,owner_name,email_id,contact_number,alternate_number,address,city,website) VALUES 
                                ('".$_SESSION['login_id']."','".$checkbox."','".$checkbox1."','".$hostel_name."','".$ownername."','".$email."','".$contact_no."','".$phonealternate."','".$address."','".$searchTextField."','".$hostel_website."')") or die(mysql_error());        
}
if(isset($_POST['add_hostel']))
{   
    extract($_POST);
    $hostel_id = mysql_query("SELECT LAST_INSERT_ID()");
    $checkbox2 = implode(',',$_POST['checkbox2']);
    $checkbox3 = implode(',',$_POST['checkbox3']);
    $insert_details1 = mysql_query("INSERT INTO `hostel_features` (hostel_id,facilities_info,food_type,breakfast,lunch,dinner,special) VALUES 
                                ('".$hostel_id."','".$checkbox2."','".$checkbox3."','".$breakfast."','".$lunch."','".$dinner."','".$special."')") or die(mysql_error());
}

php mysql扩展在php 5.5.0时已弃用,在php 7.0.0时已被删除

请使用mysqli扩展名,它允许您访问MySQL 4.1及以上版本提供的功能。

你可以用insert_id

来解决你的问题
$hostel_id = $mysqli->insert_id; // or  mysqli_insert_id() for procedural style

如果出于某种原因你想继续使用mysql,那么程序方法几乎是相同的:

$hostel_id = mysql_insert_id();