Solved

php mysql insert into two different table with one form

Posted on 2013-02-03
6
436 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 83

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 110

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 110

Accepted Solution

by:
Ray Paseur earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime is disastrous for companies and can lead to major hits on a brand, reputation, an…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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