Solved

php mysql insert into two different table with one form

Posted on 2013-02-03
6
435 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

792 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