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

  • Home
  • SEARCH
  • 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 6584297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:09+00:00 2026-05-25T16:31:09+00:00

How to insert data into two or more tables? Currently I am using this

  • 0

How to insert data into two or more tables?

Currently I am using this trick. (Let just assume that the syntax is correct)

$Sql1 = "insert into user";
$Sql2 = "insert into files";
$Sql3 = "insert into messages";

query $Sql1
query $$ql2
query $Sql3

So I am using this and I need to optimize.

Also, I think I need to use Foreign key? But I don’t know how to do it on phpmyadmin.

and lastly:

This is all I want.

To insert data on the “user” table and then automatically the table “messages” and “files”
will have also an ID which is the same with the user.. That is why I use to 3 queries in my code.

Please help me.

Thanks in ADVANCE!

myb

  • 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-25T16:31:10+00:00Added an answer on May 25, 2026 at 4:31 pm

    It is perfectly fine to use multiple queries.

    However, you should wrap the 3 queries into one transaction. That is, if one query fails for some reason, the effects of all previous queries (if any) can be rolled back so that your database stays in a consistent state.

    Note: Transactions are not possible with MySQL’s MyISAM tables. Additionally you have to use PHP’s Mysqli (or PDO) to be able to use transactions. (I assume that you’re not using some high-level data base abstraction layer.)

    As to your question regarding foreign keys: As far as I know, it makes no difference when you’re using the MyISAM engine for the tables in your database (which I presume), because MyISAM doesn’t even support the explicit modeling of foreign keys. Other table engines like InnoDB, however, can use foreign keys to check certain constraints like "a message must always be associated with an existing person".

    Here are some nice examples regarding foreign keys: http://www.postgresql.org/files/documentation/books/aw_pgsql/node131.html

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

Sidebar

Related Questions

I am trying to select data from two tables and insert it into another
How do you insert data into a MySQL date or time column using PHP
How i can insert data into more than one table of an Access database
How i insert data into xml file using Windows Application in .net?
First of all, let me just say that I'm using the PHP framework Yii,
I want to insert data into a table where I don't know the next
I'm trying to write an extension method to insert data into a dictionary of
I am trying to import a csv file to insert data into an existing
I was trying to insert new data into an existing XML file, but it's
I have to read data from some files and insert the data into different

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.