How do you insert data into two tables?
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?
I'm thinking of using transactions?
What do you think?
What do you think?
- 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?
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?
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?
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?
How much trouble am I going to get myself into by not using them?
Warning: I have no idea what I'm talking about.
Re: How do you insert data into two tables?
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.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?
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?
What is the syntax of a transaction?
Re: How do you insert data into two tables?
Depends on what database engine you're using. For MySQL, see http://dev.mysql.com/doc/refman/5.0/en/commit.htmlklevis miho wrote:What is the syntax of a transaction?
-
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?
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:
- 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?
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"
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?
Yes I know that, but this is a MySQL syntax, how to do it with php?
- 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?
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?
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
- 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?
Here is how to connect and make queries:klevis miho wrote:mysql
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)