Page 1 of 2
How do you insert data into two tables?
Posted: Mon Nov 15, 2010 9:50 am
by klevis miho
I'm thinking of using transactions?
What do you think?
Re: How do you insert data into two tables?
Posted: Mon Nov 15, 2010 12:09 pm
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.
Re: How do you insert data into two tables?
Posted: Tue Nov 16, 2010 2:52 am
by klevis miho
Yes I did a research and I'll use transactions.
Re: How do you insert data into two tables?
Posted: Tue Nov 16, 2010 4:11 pm
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?
Re: How do you insert data into two tables?
Posted: Tue Nov 16, 2010 7:16 pm
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!
Re: How do you insert data into two tables?
Posted: Mon Nov 22, 2010 5:17 am
by klevis miho
What is the syntax of a transaction?
Re: How do you insert data into two tables?
Posted: Mon Nov 22, 2010 12:41 pm
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
Re: How do you insert data into two tables?
Posted: Mon Nov 22, 2010 12:59 pm
by klevis miho
Yes MySQL, but can you guide me on how to do this with php?
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 7:58 am
by klevis miho
Anyone?
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 10:13 am
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"
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 10:25 am
by klevis miho
Yes I know that, but this is a MySQL syntax, how to do it with php?
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 11:38 am
by Christopher
What database extension are you using? PDO? mysqli? mysql?
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 3:19 pm
by klevis miho
mysql
Re: How do you insert data into two tables?
Posted: Tue Nov 23, 2010 7:41 pm
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
Re: How do you insert data into two tables?
Posted: Wed Nov 24, 2010 1:57 am
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")