php mysql insert into two different table with one form

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 .
LVL 1
AFIF JABADOAsked:
Who is Participating?
 
Ray PaseurCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
karunamoorthyCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
AFIF JABADOAuthor Commented:
the insertion in the second table need the primary key of company id ..
to insert in the second table as foreinkey.
0
 
Ray PaseurCommented:
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
 
AFIF JABADOAuthor Commented:
Thank Ray please can you provide me a simple tutorial about tranaction or lock in mysql using phpmyadmin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.