I am attempting to place information into multiple tables inside a database, i've handled to have it to operate by using this:

  $query = "INSERT INTO users (grp, email, college_id, tutor, year, password, register_date) VALUES ('$g', '$e', '$ci', '$tu', '$y', PASSWORD('$p'), NOW() )";
  $query2 = "INSERT INTO unit_26 (college_id) VALUES ('$ci')";
  $result = mysql_query ($query); // Run the Query Now woooo. 
  $result2 = mysql_query ($query2); // Run the Query Now woooo. 
  if ($result) { // If it Ran OK.

Even though it works and the details are put into both tables, I had been just wondering if anybody has an easy method to do this, or of by doing this is wrong?


As these two card inserts are performed individually, another program running at the same time might begin to see the database in a condition in which the first place is performed however the second is not.

Whether this can be a problem or otherwise is dependent around the application logic. Inside your situation it's difficult to inform without more information. Most likely not. An economic transactions including two accounts is definitely an example where this is an issue: you wouldn't want the sum of the all account balances to become wrong anytime.

If you feel you'll need this, you may make the operation atomic at the expense of performance: another program will either begin to see the database prior to the first place, or following the second place. It really works such as this:

$result = FALSE;
if (mysql_query('BEGIN')) {
    if (mysql_query($query1) &&
        $result = mysql_query('COMMIT'); // both queries looked OK, save
        mysql_query('ROLLBACK'); // problems with queries, no changes

The storage engine needs to support transactions, i.e., it needs to be InnoDB. Otherwise this can quietly not work.

SQL card inserts are for any single table only. You cannot place data into several tables simultaneously having a single Place query. You are able to pressure the separate insertions to become atomic using a transaction, but you've still got to complete an Place for every table.

May be nice should you could so something similar to

INSERT INTO t1.id, t2.val, t3.other VALUES ($t1id, $t2val, $t3other);

but alas...