Page 2 of 2

Re: How do you insert data into two tables?

Posted: Wed Nov 24, 2010 3:09 am
by klevis miho
Can you please give me a simple transaction template with php?

Re: How do you insert data into two tables?

Posted: Wed Nov 24, 2010 3:34 am
by Christopher
All the basic code you need is provided in the manual pages for mysql_connect() and mysql_query(). You need to get that code working first before you can use transactions. Once you have that code working then you just make the queries I provided in my previous post before and after your multiple inserts.

Re: How do you insert data into two tables?

Posted: Wed Nov 24, 2010 5:26 am
by klevis miho
OK I did this:

Code: Select all

mysql_query("START TRANSACTION");
$sql1 = "INSERT INTO domain1(domain, user_id) VALUES('$domain', $user_id)";
$query1 = mysql_query($sql1);
$dom_id = mysql_insert_id($query1);
$sql2 = "INSERT INTO domain2(domain_id, user_id) VALUES($dom_id, $user_id)";
$query2 = mysql_query($sql2);
if($query1 && $query2) {
	echo 'Domain inserted successfully.';
	mysql_query("COMMIT");
} else {
	mysql_query("ROLLBACK");
}
But it does not insert anything, do you know what I am doing wrong?

Re: How do you insert data into two tables?

Posted: Wed Nov 24, 2010 5:31 am
by klevis miho
Nevermind, I was doing this:
$dom_id = mysql_insert_id($query1);

wrong.


Thank you for your help, it was easier than I thought :)