Solved

php mysql insert into two different table with one form

Posted on 2013-02-03
6
441 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

695 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