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 6337079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:11:02+00:00 2026-05-24T19:11:02+00:00

I have the following tables for a php mysql based quiz application: QUESTION id

  • 0

I have the following tables for a php mysql based quiz application:

QUESTION
id
text
...

ANSWER
id
question_id
text
correct (true/false)
...

TAG
id
text

QUESTION_TAG
question_id
tag_id

I’m trying to create one form from which I can input a new question and it’s answer choices, mark one choice as correct, give any number of tags to the question (just like the tag feature on stackoverflow), for existing tags just creating the linking entity, and for new tags creating the tag entity and the linking entity, then hit one submit button to insert all of it to the various tables.

I’m running into trouble because many of the tables are using an auto-increment primary key and I don’t know how I can programmatically refer to those entities when I don’t yet know what the id will be. Does that make sense? Maybe I am overlooking something obvious here.

For example, if I only wanted to insert a new question, I would just insert the QUESTION.text, and the QUESTION.id would then be auto assigned. If I want to insert the answers for that question together with it, how do I assign ANSWER.question_id before QUESTION.id is known?

I thought of different things like counting how many entities exist in QUESTION, and then giving the next ANSWER.question_id the highest value + 1, or for the ANSWER.question_id running a select from QUESTION.text for the text that was just entered, but those ideas don’t seem very solid. What’s the best way to do this?

  • 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-05-24T19:11:04+00:00Added an answer on May 24, 2026 at 7:11 pm

    If you are using PHP’s mysql extension, mysql_insert_id() is what you want; if using PDO, then $dbh->lastInsertId().

    You will need to insert the records in the order of their relationships, starting with the table that has no foreign keys. You will insert the parent records first, then its children, then their children, and so on. Each time, record the last inserted ID in a variable so you can refer to it when inserting its child records.

    So, you will:

    1. Insert the QUESTION record.
    2. Record the last inserted ID.
    3. Insert the ANSWER records, using ID recorded in step 2 as question_id.
    4. And so on…
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have a heap based table in MySQL that I am trying to update
I need some help with query from multiple tables. My database: I have following
Imagine the following problem: You have a database containing about 20,000 texts in a
My aim is to have a simple, form based CMS so the client can
This might come across as a simple question, but I have very limited experience
I have two tables job which contains an attribute called employer_id_job that links it
I maintain a custom built CMS-like application. Whenever a document is submitted, several tasks
I'm familiar with a whole bunch of ways to authenticate users for the web-based
I have a table with details on personnel. I would like to create a

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.