【发布时间】:2015-01-10 21:10:01
【问题描述】:
我希望 php 在提交事务之前冻结,以便在提交事务之前向用户显示更新日志并提供继续或回滚的选项。
这是将更新提交到数据库的代码:-
<?php
include 'submitLogger.php';
// Begin logging
ini_set( "error_log", $logFile );
ini_set( "log_errors", "On" );
ini_set( "display_errors", "Off" );
error_log( "Log file '" . $logFile . "' created" );
// Open the database
.
.
error_log( "Connect OK" );
.
.
error_log( "Transaction started (autocommit OFF)\n" );
.
.
error_log( "Processing " . count( $deletes ) . " item(s) marked for deletion..." );
.
.
// commit changes
error_log( "Committing changes..." );
if ( mysqli_commit( $link ) === false ) {
mysqli_rollback( $link );
error_log( "Commit failed. Transaction rolled back." );
$response['error'] = "Could not commit changes. Transaction rolled back.";
} else {
error_log( "Commit successful!" );
$response['success'] = "Success!";
}
// close DB connection
.
.
// Return result
.
.
这是加载上面更新代码然后显示日志文件的代码,但是在事务提交之后:-
<script type="text/javascript">
$(document).ready( function() {
// send AJAX request to perform the updates and begin logging
$.post(
'../lib/updateMenu.php',
sendData,
function( response ) {
// was it successful?
if ( typeof response.success === 'undefined' ) {
// no - show alert
if ( response.error ) {
alert( response.error );
}
console.error( "Amend not successful" );
console.error( response );
return;
}
// delete the AmendmenuAmendselected program window to force a reload on next click
$( "#programWindowAmendmenuAmendselected" ).remove();
},
"json"
).error(function(jqXHR, textStatus, errorThrown) {
alert( 'Unexpected error: ' + textStatus + ' ' + errorThrown );
console.error( 'Unexpected error during amend: ' + textStatus + ' ' + errorThrown );
console.error(jqXHR);
}).complete(function() {
// once a reply is received, stop the logging
ajaxLogtail.stopTail();
});
// begin querying log file
var ajaxLogtail = new AjaxLogtail( '../lib/ajaxLogtail.php?logfile=' + logFile, "programWindowAmendmenuSubmit" );
ajaxLogtail.startTail();
});
</script>
如何让 php 代码在提交事务之前等待前端的响应?
请问有人有什么好主意可以帮忙吗?
【问题讨论】:
-
不可能。这不是http的工作方式。您不能“冻结” php 然后恢复,也不能在用户提示需要的单个 http 请求中执行客户端服务器之间的多次往返
-
@Marc B,我可以运行两次交易吗?第一次作为诱饵向用户显示日志,第二次是真实的,如果用户选择不显示日志?很乱,有什么好办法吗?
-
并非如此。当一个 php 脚本退出时,将执行清理并且您的数据库连接将被关闭并且事务回滚。如果您尝试使用持久数据库连接,则无法保证来自用户的响应会获得相同的连接,并且您最终会得到一个完全不同的连接,其中没有等待事务。
标签: php jquery logging transactions commit