Reputation: 95
I have created two different tables for the one form. The upper part of form is inserted into one table and below part of form is inserted into another table. I have to take the Primary Id of first table as the Foreign Id for Another table.
Is it possible? and is so then how? Here is the query code of 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());
}
Upvotes: 0
Views: 43
Reputation: 1951
The php mysql extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
Instead use the mysqli extension which allows you to access the functionality provided by MySQL 4.1 and above.
You can solve your problem with insert_id
$hostel_id = $mysqli->insert_id; // or mysqli_insert_id() for procedural style
If for some reason you want to stick with mysql the procedural way is almost the same:
$hostel_id = mysql_insert_id();
Upvotes: 1