简体   繁体   English

在提交事务之前显示日志文件。 仅在用户确认后提交

[英]Display logfile before committing transaction. Only commit if user confirms

When I update records in the data I want to show the user the update log BEFORE the transaction is committed and give the option to continue or roll back. 当我更新数据中的记录时,我想向用户显示更新日志,然后再提交事务,并提供继续或回滚的选项。

The code that commits the updates to the database follows this format:- 将更新提交到数据库的代码遵循以下格式:

<?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
.
.

This is the code that loads the update code above and then displays the logfile, but AFTER the transaction has been committed:- 这是在上面加载更新代码,然后显示日志文件的代码,但是在事务提交后:

<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>

I cannot figure out how to split this so that I can display the logfile and then, if there weren't any update errors, commit the transaction. 我无法弄清楚如何拆分它,以便可以显示日志文件,然后,如果没有任何更新错误,则提交事务。

Does anyone have a neat idea to help, please? 请问有人有什么好主意可以帮助您吗?

As long as i know you cannot do it. 只要我知道你做不到。 At least not with php. 至少不是用PHP。 Php is a server side, script code runned. PHP是服务器端,脚本代码已运行。 It runs from the first line to the last line, stop. 它从第一行到最后一行停止。

You however can run a select and tell how many records will be at example deleted, give those data to the ajax and then on confirm do the real delete operation (and commit the change). 但是,您可以运行select并告诉您在示例中将删除多少条记录,将这些数据提供给ajax,然后在确认时执行真正的删除操作(并提交更改)。

However you can (if your web app is used by a lot of people) have a situation where your ajax find 25 element to be deleted and the real delete query will delete 26 or more. 但是,您可能会遇到这种情况(如果您的Web应用程序被很多人使用),您的Ajax会发现要删除的25个元素,而真正的删除查询将删除26个或更多。 For that better re-think your logic a little and produce a log after the delete query. 为此,请重新考虑一下您的逻辑,并在删除查询后生成一个日志。

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

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