简体   繁体   English

PHP MySQL事务未插入所有查询

[英]Php mysql transaction not inserting all queries

I got a problem. 我有问题 I run a transaction containing 2 queries but only one query is writing on database.. The other is not. 我运行一个包含2个查询的事务,但是只有一个查询正在数据库中写。另一个则不是。 How is that possible ? 那怎么可能呢? I thought it was either both or none, that's the point of using a transaction right ? 我以为这两者都是或都不是,这就是使用交易权的关键吗?

Here is part of my code : 这是我的代码的一部分:

mysqli_autocommit($conn, FALSE);
mysqli_query($conn, "INSERT INTO my_table_exemple (orderId, packageName, productId, purchaseTime, purchaseState, purchaseToken) 
                             VALUES ('".$_POST["orderId"]."', '".$_POST["packageName"]."', '".$_POST["productId"]."', '".$_POST["purchaseTime"]."', ".$_POST["purchaseState"].", '".$_POST["purchaseToken"]."')");

mysqli_query($conn, "UPDATE user
                             SET end_date='".$calcEndDate."'
                             WHERE id=".$_POST['user_id']);

/* Validation de la transaction */
if (!mysqli_commit($conn)) {
    $row_array['status'] = "error";
    $row_array['msg'] = "Error: " . mysqli_error($conn);
} else {
    $row_array['status'] = "success";
    $row_array['msg'] = "Transaction success";
}   
echo json_encode($row_array);

This is returning transaction success, updating user table but no record inserted in my_table_exemple. 这将返回事务成功,更新用户表,但my_table_exemple中未插入任何记录。

I have an error on my insert query I guess, I'm going to debug it but I don't want to go ahead without knowing why my transaction is not working the way it is supposed to. 我猜我的插入查询有一个错误,我要调试它,但是我不想知道为什么我的事务无法按预期方式运行。

You need to check after each query for any errors - on any error you would rollback, if no errors after all of your queries you would commit. 您需要在每个查询之后检查是否有任何错误-在您将回滚的任何错误上,如果在提交所有查询之后都没有错误,则需要提交。 Only the queries with no error will be committed & the commit reports any errors on the commit - not preceding queries. 只有没有错误的查询才会被提交,并且提交将报告有关提交的任何错误-而不是先前的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM