How do you insert data into two tables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

How do you insert data into two tables?

Post by klevis miho »

I'm thinking of using transactions?
What do you think?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you insert data into two tables?

Post by Christopher »

You use two INSERTs to insert into two tables. Transactions are a way to deal with errors that might occur during one of the INSERTs. You could also check the results of the queries and delete or retry manually. Your choice.
(#10850)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

Yes I did a research and I'll use transactions.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How do you insert data into two tables?

Post by thinsoldier »

Lets say I have a project where I need to insert into 7 tables and I'm not using transactions.

How much trouble am I going to get myself into by not using them?
Warning: I have no idea what I'm talking about.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How do you insert data into two tables?

Post by califdon »

thinsoldier wrote:Lets say I have a project where I need to insert into 7 tables and I'm not using transactions.

How much trouble am I going to get myself into by not using them?
Depends on a whole bunch of things. If your server, the code you write, your Internet connection, and the weather forecast are all perfect and never experience a failure, you have nothing to worry about.

The point of a transaction is to assure that if ANY part of the transaction is processed, ALL parts of that transaction are processed. So if anything breaks down between the beginning and the end, the system will restore all earlier parts of the transaction as if they had never occurred.

If you are inserting or updating 7 tables doesn't care if 6 out of the 7 transactions processes, you don't gain anything by using transactions. But if you want to debit your bank account and credit mine, and only the first part happens, you'll lose money!
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

What is the syntax of a transaction?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How do you insert data into two tables?

Post by califdon »

klevis miho wrote:What is the syntax of a transaction?
Depends on what database engine you're using. For MySQL, see http://dev.mysql.com/doc/refman/5.0/en/commit.html
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

Yes MySQL, but can you guide me on how to do this with php?
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

Anyone?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you insert data into two tables?

Post by Christopher »

There are examples on this page: http://dev.mysql.com/doc/refman/5.0/en/commit.html

PS - this was the first result from googling "mysql transactions"
(#10850)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

Yes I know that, but this is a MySQL syntax, how to do it with php?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you insert data into two tables?

Post by Christopher »

What database extension are you using? PDO? mysqli? mysql?
(#10850)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do you insert data into two tables?

Post by klevis miho »

mysql
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How do you insert data into two tables?

Post by califdon »

All that PHP does is connect to the database, then send the SQL to MySQL. Learn how to do those 2 things and you've got it. A good tutorial is here: http://www.w3schools.com/php/php_mysql_intro.asp
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you insert data into two tables?

Post by Christopher »

klevis miho wrote:mysql
Here is how to connect and make queries:

http://us2.php.net/manual/en/function.mysql-connect.php
http://us2.php.net/manual/en/function.mysql-query.php

To do transactions you first do a mysql_query("START TRANSACTION") before other queries, and then at the end do either mysql_query("COMMIT") or mysql_query("ROLLBACK")
(#10850)
Post Reply