2015-10-04 25 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(); 

谢谢大家!