2015-10-04 74 views
0

我對mysql事務有些懷疑。 我需要在兩個不同的表中同時創建兩個記錄,如果其中一個插入失敗,另一個不能保存。 這裏是我的代碼:Php mysql事務回滾疑惑

$conn->autocommit(FALSE); 
$conn->query("START TRANSACTION"); 

// Insert values 
$conn->query("INSERT INTO cards (id, points, reg_date, last_update) VALUES ('','$points', '$reg_date', '$reg_date')"); 
$card_id = $conn->insert_id; 

$conn->query("INSERT INTO students (id, firstname, lastname, email, telephone, birthdate, address, city, cap, fiscal_code, username, card_id, password, token_password, reg_date, is_active) 
    VALUES ('','$student->firstname', '$student->lastname', '$student->email', '$student->telephone', '$student->birthdate', '$student->address', '$student->city', '$student->cap', '$student->fiscal_code', '$student->username', '$card_id','$student->password', '$student->token_password', '$student->reg_date', '$student->is_active')"); 

// Commit transaction 
if (!$conn->commit()) { 
    print("Transaction commit failed\n"); 
    $conn->rollback(); 
} 
$conn->close(); 

$康恩鏈接包含的文件中創建的,我需要的是$因爲card_id的是在第二個查詢的值的外鍵。 問題是:如果第一個查詢失敗,一切正常,所以沒有記錄插入我的數據庫。但是,如果第二個查詢失敗,回滾將不起作用,第一個查詢中的記錄將保存在數據庫中。 編輯:我正在使用InnoDB。 我在哪裏做錯了?謝謝。

+0

您正在使用哪種數據庫引擎? – sandeepsure

+0

我忘了寫它,但我使用InnoDB。 – Ali

回答

0

首先確保您的DB ENGINE是InnoDB。

function begin(){ 
mysql_query("BEGIN"); 
} 

function commit(){ 
mysql_query("COMMIT"); 
} 

function rollback(){ 
mysql_query("ROLLBACK"); 
} 

mysql_connect("localhost","root", "") or die(mysql_error()); 

mysql_select_db("test") or die(mysql_error()); 

begin(); // transaction begins 
// Insert values 
mysql_query("INSERT INTO cards (id, points, reg_date, last_update) VALUES ('','$points', '$reg_date', '$reg_date')"); 
$card_id = $conn->insert_id; 

mysql_query("INSERT INTO students (id, firstname, lastname, email, telephone, birthdate, address, city, cap, fiscal_code, username, card_id, password, token_password, reg_date, is_active) 
VALUES ('','$student->firstname', '$student->lastname', '$student->email', '$student->telephone', '$student->birthdate', '$student->address', '$student->city', '$student->cap', '$student->fiscal_code', '$student->username', '$card_id','$student->password', '$student->token_password', '$student->reg_date', '$student->is_active')"); 


$result = mysql_query($query); 

if(!$result){ 
rollback(); // transaction rolls back 
echo "transaction rolled back"; 
exit; 
}else{ 
commit(); // transaction is committed 
echo "Database transaction was successful"; 
} 

?> 

希望這會有所幫助。

+0

謝謝,我會嘗試一下,但我需要以面向對象的方式實現它 – Ali

0

好吧,我找到了解決辦法,這裏是工作代碼:

$conn->autocommit(FALSE); 
$conn->query("START TRANSACTION"); 

// Insert values 
$res=$conn->query("INSERT INTO cards (id, points, reg_date, last_update) VALUES ('','$points', '$reg_date', '$reg_date')"); 
$card_id = $conn->insert_id; 

$res1= $conn->query("INSERT INTO students (id, firstname, lastname, email, telephone, birthdate, address, city, cap, fiscal_code, username, card_id, password, token_password, reg_date, is_active) 
    VALUES ('','$student->firstname', '$student->lastname', '$student->email', '$student->telephone', '$student->birthdate', '$student->address', '$student->city', '$student->cap', '$student->fiscal_code', '$student->username', '$card_id','$student->password', '$student->token_password', '$student->reg_date', '$student->is_active')"); 

// Commit transaction 
if ($res and $res1) { 
    $conn->commit();; 
} else {   
    $conn->rollback(); 
} 
$conn->close(); 

謝謝大家!