当我更新数据中的记录时,我想在提交事务之前向用户显示更新日志,并提供继续或回滚的选项。
将更新提交到数据库的代码遵循以下格式:-
<?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。Php是服务器端,脚本代码运行。它从第一行运行到最后一行,停止。
但是,您可以运行选择并告诉在示例删除时将删除多少条记录,将这些数据提供给 ajax,然后在确认执行真正的删除操作(并提交更改)时。
但是,您可能会(如果您的 Web 应用程序被很多人使用)遇到这样的情况:您的 ajax 找到要删除的 25 个元素,而真正的删除查询将删除 26 个或更多。为了更好地重新考虑您的逻辑,并在删除查询后生成日志。