Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:17:38+00:00 2026-06-08T07:17:38+00:00

I have two tables: Customer with the field ID which is auto increment and

  • 0

I have two tables:

Customer with the field ID which is auto increment and the Primary Key and

address with the field customer_ID which is a foreign key .

I have a form which asks for all of the customer info and their addresses ( can be more than 1)

I run the following two query’s to update the database however I am currently inputting the value for Customer_ID manually to test it.

mysql_query("INSERT INTO customer(First_Name, Surname, Company_Name, Telephone, Alt_Telephone)VALUES('$fName', '$lName', '$cName', '$tel', '$altTel')") ;
mysql_query("INSERT INTO address(line_1, line2, town, postcode, customer_ID)VALUES('$line1', '$line2', '$town', '$post_code', '2')") ;

The customer_ID field should be the same as the current ID field from the customer table how can I make sure that when the address table is filled in it is doing this automatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:17:39+00:00Added an answer on June 8, 2026 at 7:17 am

    Using PHP

    The easiest solution to your problem would be to call mysql_insert_id in your php-code after your first call to mysql_query (inserting data into your table customer).

    This function will return the last id inserted, though you’ll have to make sure that the id of customer is a primary key which is auto-incremented.

    The value returned by mysql_insert_id is local to the given connection/session, even if another connection inserts a record after you did you’ll get the id inserted by you.


    Using MySQL

    You could use the SQL-function LAST_INSERT_ID() as the below, just make sure that the previous sql-query ran was the insert into customer (as well as checking so that the id is indeed a key and auto-incremented).

    -- first insert into `customer`
    
    INSERT INTO address
      address(line_1, line2, town, postcode, customer_ID)
    VALUES
      ('$line1', '$line2', '$town', '$post_code', LAST_INSERT_ID());
    

    Same thing here as with the php-version, LAST_INSERT_ID() will retrieve the last inserted ID by you.


    You could also use a SQL feature which could be described as SELECT INTO, see the below snippet:

    INSERT INTO address
     address(line_1, line2, town, postcode, customer_ID)
    SELECT
      '$line1', '$line2', '$town', '$post_code', customer.id
    FROM customer
    WHERE <some_constraint>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one with products and a foreign key for the customer.
I have two tables CustomerBalance and Customer which are bound with CustomerRefId field. I
I have a table which uses two columns to represent its primary key, a
I have the following two tables: Customer { int Id int Name } Bills
I have two tables in my database that look like that: Customer: C_ID city
I have two tables, Customer and CustomerPhone. Customer usually has multiple phone numbers, so
I have two tables as follows: Customer | Product ------------------ A | Car A
I have two tables, Customer and CustomerConfiguration that should have a one-to-one mapping. What
If I have two tables, Customers and Orders, and I want to look up
I have two tables images2 and image_data So the goal is to have 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.