Mysql
 sql >> Database >  >> RDS >> Mysql

Inserimento di più tabelle con transazione in mysql

Usa mysql_insert_id() se stai percorrendo quel percorso.

<?
mysql_query("START TRANSACTION");

$q1 = mysql_query("INSERT INTO table A (id, firstName, lastName) VALUES (?, ?, ?)");

// This is your baby. The id of the last record inserted
$last_inserted_id = mysql_insert_id();

$q2 = mysql_query("INSERT INTO table b (id, id from A, xyz) VALUES (?, ?, ?)");

// If query1 and query2 succeeded, commit changes to your database
// Creates both records
if ($q1 && $q2) {
    mysql_query("COMMIT");
}
else {        
    // Else initiate a rollback, and no records are committed.
    mysql_query("ROLLBACK");
}

?>