RE: [PHP-DB] 2 Tables- 1 Insert Problem
Beyond that excellent answer, this is exactly the kind of situation where transactions would be helpful (and in a lot of cases, mission critical). So an expanded quasi-pseudocode rendition of olinux's answer could be: $result_bt = mysql_query('[whatever it takes to begin a transaction]'); $result_1 = mysql_query($sql1); if (!$result_1) { $result_rb = mysql_query('[whatever it takes to rollback a transaction']); /* whatever else you want to do, such as show an error message */ } else { $result_2 = mysql_query($sql2); if (!$result_2) { $result_rb = mysql_query('[whatever it takes to rollback a transaction']); /* whatever else you want to do, such as show an error message */ } else { $result_et = mysql_query('[whatever it takes to commit a transaction']); } } If your implementation of MySQL supports transactions (and if it doesn't then either change databases or web hosts), use them. It helps ensure the integrity of the data. Please feel free to be more elegant in the error checking...the above is just an example and would undoubtedly not be used, even fixed up for syntax, on a production site. Doug At 10:25 PM 7/28/01 -0500, olinux wrote: Not possible. do this $result_1 = mysql_query($sql1) or die('Insert 1 failed'); do this $result_2 = mysql_query($sql2) or die('Insert 2 failed'); olinux -Original Message- From: Steve Fitzgerald [mailto:[EMAIL PROTECTED]] I'm trying to insert data into two separate tables using 1 INSERT. ... snip ... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP-DB] 2 Tables- 1 Insert Problem
I'm trying to insert data into two separate tables using 1 INSERT. The code below represents a first crack at it, but I can't seem to figure out how to get this to work properly. Thanks. Steve ? $db_name=testDB; $table_name1 = my_contacts; $table_name2= company; $connection = @mysql_connect (,,or die (Couldn't connect.); $db = @mysql_select_db($db_name, $connection) or die (Couldn't select database.); $sql_1 = INSERT INTO $table_name1 (id,fname,lname,address1,address2,address3,postcode,country,prim_tel,sec_tel ,email,birthday) VALUES (\\, \$fname\,\$lname\,\$address1\,\$address2\,\$address3\,\$postcode\ , \$country\,\$prim_tel\,\$sec_tel\,\$email\,\$birthday\) ; $sql_2 = INSERT INTO $table_name2 WHERE my_contacts.company=$companyid (companyname,website) VALUES (\\, \$companyname\,\$website\) ; $result = @mysql_query ($sql_1,$sql_2,$connection) or die (Couldn't execute query.); ? -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] 2 Tables- 1 Insert Problem
Not possible. do this $result_1 = mysql_query($sql1) or die('Insert 1 failed'); do this $result_2 = mysql_query($sql2) or die('Insert 2 failed'); olinux -Original Message- From: Steve Fitzgerald [mailto:[EMAIL PROTECTED]] I'm trying to insert data into two separate tables using 1 INSERT. The code below represents a first crack at it, but I can't seem to figure out how to get this to work properly. Thanks. Steve _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]