1

I have this two tables: enter image description here

I also have a dynamic form in which it contains table and the user can add rows and the data from it will be inserted in tblcamealsformdetails but the basis for inserting it is the id from tblcamealsform. How do I insert all the values to the two tables simultaneously?

Here's my form: meal form

1

2 Answers 2

3

You will enter data first in table tblcamealsform. You insert ID from that query. That ID you will use then to insert the rest of the data, along with the insert ID, in table tblcamealsformdetails.

So you don't do it simultaniously. You add the dependencies first.

To get the insert-id from the last query you executed, you will need mysql_insert_id(). See http://php.net/manual/en/function.mysql-insert-id.php

In answer to the comment what will happen if multiple users use the form at the same time:

Since you open a mysql connection at the top of your script which will result a unique connection pointer and all of the mysql-functions you call automatically reference to that pointer I think mysql_insert_id() will always reference to the latest query performed by the current connection. So another thread by another user would not interfere with this.

Please note that I am not 100% sure about this though.

Anyway: I am using this for about 10 years now some of which include high-traffic websites and I have never experienced any problems using this method, so in my opinion you can safely use it.

There is one exception to this: You must always call mysql_insert_id() immediately after executing the query you want the ID for. If you execute any other query in the meantime (for example, you call a method of another object which performs an insert-query) mysql_insert_id() will return the ID of that query instead. This is mistake I have made in the past and which you have to be aware of.

Sign up to request clarification or add additional context in comments.

5 Comments

How to do it? It will saved by one save button.
If you know how execute one insert query, you should know how to insert a second too, right? Please add the code you have written so far and can extend my answer explaining what is missing/wrong in your code.
Yeah, I know how to insert data into database. The problem is the id is autoincrement. I can't get it as basis for my insertion on the details table.
But what will happen if there are multiple user using the form?
If this is the answer you were looking for, could you accept it as the answer? Or any of the other answers ofcourse.
0

I'd like to point you using LAST_INSERT_ID:

when doing multiple-row inserts, LAST_INSERT_ID() will return the value of the first row inserted (not the last).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.