?
Solved

php mysql insert into two different table with one form

Posted on 2013-02-03
6
Medium Priority
?
445 Views
Last Modified: 2013-02-03
i have the following table :

table company ( compid ( p.k A.i ) , compname , accounting_code )
table address ( addressid , compid , addressline1 , addressline2 )

i want to insert into two table and i want to ensure the insertion will occurs in the two table
maybe the insertion occurs in the first table but in the second we have an error during executing .. test than insert

if u can provide me a code .
0
Comment
Question by:afifosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38848203
That's a little confusing.  The form isn't a problem since it just sends the data to the PHP page.  You have to separate the data for the two tables though and make two separate inserts.  You can't do them both in one operation.
0
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 38848228
First update first table and get the success status then update the second table. If during the update in second table, success status fails then rollback the updation in the first table also.
I hope you got it.
0
 
LVL 1

Author Comment

by:afifosh
ID: 38848349
the insertion in the second table need the primary key of company id ..
to insert in the second table as foreinkey.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38848702
After an INSERT you can get the AUTO_INCREMENT PRIMARY KEY value with something like this:

$id  = $mysqli->insert_id;

The assumption here is that $mysqli is the data base connection object.  You MUST acquire the insert_id property immediately after the query and before any other SQL operations are run.

If there is any chance of a race condition, you probably want to consider using a transaction or LOCK TABLES around these two INSERT statements.

Best regards, ~Ray
0
 
LVL 1

Author Comment

by:afifosh
ID: 38848836
Thank Ray please can you provide me a simple tutorial about tranaction or lock in mysql using phpmyadmin
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38848856
I don't think phpMyAdmin is necessarily the right tool.

This tells about MySQL Transactions.  If you want to be able to roll back the INSERT, then table locking is not really adequate by itself.
http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html
http://dev.mysql.com/doc/refman/5.0/en/commit.html
http://www.sitepoint.com/mysql-transactions-php-emulation/

HTH, ~Ray
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to dynamically set the form action using jQuery.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question