【问题标题】:Display logfile before committing transaction. Only commit if user confirms在提交事务之前显示日志文件。仅在用户确认时提交
【发布时间】:2015-01-09 12:35:42
【问题描述】:

当我更新数据中的记录时,我想在事务提交之前向用户显示更新日志,并提供继续或回滚的选项。

将更新提交到数据库的代码遵循以下格式:-

<?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 jquery logging transactions commit


【解决方案1】:

只要我知道你做不到。 至少不是用php。 php是服务器端,运行脚本代码。 从第一行一直跑到最后一行,停止。

但是,您可以运行选择并告诉示例将删除多少记录,将这些数据提供给 ajax,然后确认执行真正的删除操作(并提交更改)。

但是,您可以(如果您的 Web 应用程序被很多人使用)遇到这样的情况:您的 ajax 找到要删除的 25 个元素,而真正的删除查询将删除 26 个或更多。为了更好地重新考虑您的逻辑,并在删除查询后生成日志。

【讨论】:

  • 正确的 Marco,似乎没有一个简单的解决方案。更新不会很多,因为只有管理员会使用此功能,但您的观点仍然有效,实际更新可能最终与日志不同。
  • 请注意,您不能停止(在 php 中)事务,如果未提交,它将以回滚结束,事务状态与其他语言不同(会有其他麻烦)。
  • 我怀疑我必须在我的 php 代码中创建一些包含 javascript 提示的 html,该提示将显示日志并询问用户是否要继续。可能在 "error_log("Committing changes..." );" 之后陈述。我只是不确定如何从用户那里获得回复。马可有什么想法吗?
  • 您可以创建两个 Ajax 函数。第一个询问“已选择”元素的选择,因此是 proto-delete,它将告诉管理员将删除多少行。在这个页面中,一个按钮将允许真正的删除。
  • OtherWise 你只能通过 id 删除,你可以创建一个具有元素 id 的数组并用 ajax 传递它来对每个数组元素执行删除(从 id = $myvar 的表中删除),它会更安全。
猜你喜欢
  • 2017-01-08
  • 2022-11-18
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2014-09-22
  • 2013-09-22
  • 2015-03-11
  • 2015-01-10
相关资源
最近更新 更多