Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql事务回滚_Php_Mysql_Transactions - Fatal编程技术网

Php mysql事务回滚

Php mysql事务回滚,php,mysql,transactions,Php,Mysql,Transactions,我对mysql事务有一些疑问。 我需要同时在两个不同的表中创建两条记录,如果其中一条插入失败,则不能保存另一条。 这是我的密码: $conn->autocommit(FALSE); $conn->query("START TRANSACTION"); // Insert values $conn->query("INSERT INTO cards (id, points, reg_date, last_update) VALUES ('','$points', '$reg_

我对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();
$conn link是在包含的文件中创建的,我需要$card_id,因为它是第二个查询值中的外键。 问题是:如果第一次查询失败,一切都正常,因此不会在我的数据库中插入任何记录。但是,如果第二个查询失败,回滚将不起作用,第一个查询的记录将保存在数据库中。 编辑:我正在使用InnoDB。
我哪里做错了?谢谢。

首先确保您的数据库引擎是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";
}

?>

希望这有帮助。

好的,我找到了解决方案,下面是工作代码:

$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();

谢谢大家

你用的是哪个数据库引擎?我忘了写了,但我用的是InnoDB。谢谢,我会试试,但我需要用面向对象的方式