更新SQL Server数据库表编辑/更改

Update SQL Server Database On Table Edit/Change

本文关键字:编辑 更改 数据库 SQL Server 更新      更新时间:2023-09-26

我有一个可以用多种方式编辑的动态HTML表。有一个编辑按钮,您可以在其中编辑内联的行信息,然后单击save保存信息。一个使行变灰的停用按钮和一个随后出现的激活按钮,以便重新激活该行。还有一个添加行按钮,它会弹出一个对话框,你可以再次点击添加行,在表格中添加另一行。

然而,虽然这一切都很好……我想写这些变化/更新到一个SQL Server数据库,现在,它实际上保存它们。我希望能够在每个操作(保存、停用/激活和添加行)发生后自动保存这些更改。我从来没有这样做过,所以任何帮助/建议/代码将不胜感激!

JavaScript代码:

// ----- Deactivate/Activate Row -----
$(document).on("click", "#html_master .deactivate", function () {
  var $this = $(this);
  var $tr = $this.closest('tr');
  var action = $tr.hasClass('deactivated') ? 'activate' : 'deactivate';
  // ------ Confirmation box in order to deactivate/activate row -----
  if (confirm('Are you sure you want to ' + action + ' this entry?')) {
    $tr.toggleClass('deactivated');
    $this.val(function (i, t) {
      return t == 'Deactivate' ? 'Activate' : 'Deactivate';
    });
  }
});
// ----- Edit Row -----
$(document).on("click", "#html_master .edit", function () {
  var $this = $(this);
  var tds = $this.closest('tr').find('td').not('.mr_id').filter(function () {
    return $(this).find('.edit').length === 0;
  });
  if ($this.val() === 'Edit') {
    $this.val('Save');
    tds.prop('contenteditable', true);
  } else {
    var isValid = true;
    var errors = '';
    $('#myDialogBox').empty();
    // changed from here.......
    var elements = tds;
    if (tds.find('input').length > 0) {
      elements = tds.find('input');
    }
    elements.each(function (index, element) {
      var type = $(this).attr('class');
      var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
      // changed from here....... to here
      // ----- Switch statement that provides validation -----
      switch (type) {
        case "buyer_id":
          if (!$.isNumeric(value)) {
            isValid = false;
            errors += "Please enter a valid Buyer ID'n";
          }
          break;
        case "poc_n":
          if (value == value.match(/^[a-zA-Z's]+$/)) {
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Name'n";
          }
          break;
        case "poc_e":
          if (value == value.match(/^['w'-'.'+]+'@[a-zA-Z0-9'.'-]+'.[a-zA-z0-9]{2,4}$/)) {
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Email'n";
          }
          break;
        case "poc_p":
          if (value == value.match('^[0-9 ()+/-]{10,}$')) {
            break;
          }
          else {
            isValid = false;
            errors += "Please enter a valid Phone Number'n";
          }
          break;
      }
    })
    if (isValid) {
      $this.val('Edit');
      tds.prop('contenteditable', false);
    } else {
      alert(errors);
    }
  }
});
// ----- Dialog Box -----
$( function() {   
    var dialog, form,
      emailRegex = /^[a-zA-Z0-9.!#$%&'*+'/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:'.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
      phoneRegex = /^(?:(?:'+?1's*(?:[.-]'s*)?)?(?:'('s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])'s*')|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))'s*(?:[.-]'s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})'s*(?:[.-]'s*)?([0-9]{4})(?:'s*(?:#|x'.?|ext'.?|extension)'s*('d+))?$/,
      mr_name = $( "#mr_name" ),
      buyer_id = $( "#buyer_id" ),
      poc_n = $( "#poc_n" ),
      poc_e = $( "#poc_e" ),
      poc_p = $( "#poc_p" ),
      allFields = $( [] ).add( mr_name ).add( buyer_id ).add( poc_n ).add( poc_e ).add( poc_p ),
      tips = $( ".validateTips" );
  console.log(allFields);
    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }
    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }
    function addVendor() {
      var valid = true;
      allFields.removeClass( "ui-state-error" );
      valid = valid && checkRegexp( mr_name, /^[a-z]([0-9a-z_'s])+$/i, "Please enter a valid vendor name" );
      valid = valid && checkRegexp( buyer_id, /^(0|[1-9][0-9]*)$/, "Please enter a valid Buyer ID" );
      valid = valid && checkRegexp( poc_n, /^[a-zA-Z ]*$/, "Please enter a valid name" );
      valid = valid && checkRegexp( poc_e, emailRegex, "Please enter a valid email" );
      valid = valid && checkRegexp( poc_p, phoneRegex, "Please enter a valid phone number" );
      if ( valid ) {
        var $tr = $( "#html_master tbody tr" ).eq(0).clone();
        $.each(allFields, function(){
          $tr.find('.' + $(this).attr('id')).html( $(this).val() );
        });
        $tr.find('.mr_id').html( $( "#html_master tbody tr" ).length + 1 );
        $( "#html_master tbody" ).append($tr);
        /*
        $( "#html_master tbody" ).append( "<tr>" +
          "<td>" + mr_name.val() + "</td>" +
          "<td>" + buyer_id.val() + "</td>" +
          "<td>" + poc_n.val() + "</td>" +
          "<td>" + poc_e.val() + "</td>" +
          "<td>" + poc_p.val() + "</td>" +
        "</tr>" );
        */
        dialog.dialog( "close" );
      }
      return valid;
    }
    var dialog = $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 400,
      width: 350,
      modal: true,
      buttons: {
        "Add Row": addVendor,
        Cancel: function() {
          dialog.dialog( "close" );
        }
      },
      close: function() {
        form[ 0 ].reset();
        allFields.removeClass( "ui-state-error" );
      }
    });
    form = dialog.find( "form" ).on( "submit", function( event ) {
      event.preventDefault();
      addVendor();
    });
    $( ".create-user" ).button().on( "click", function() {
      dialog.dialog( "open" );
    });
  } );
HTML/PHP代码:

<?php
$host="xxxxxxxxx"; 
$dbName="xxxxx"; 
$dbUser="xxxxxxxxxxx"; 
$dbPass="xxxxxx";
$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM Stage_Rebate_Master ORDER BY MR_ID ASC";
?>
<html>
<body> 
<div id="dialog-form" title="Add Vendor">
  <p class="validateTips">All form fields are required.</p>
  <form>
    <fieldset>
      <label for="mr_name">Vendor</label>
      <input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all">
      <label for="buyer_id">Buyer ID</label>
      <input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all">
      <label for="poc_n">POC Name</label>
      <input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Email</label>
      <input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Phone</label>
      <input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all">
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

没有Ajax -这可以通过一个(PHP/ASP)"直通"窗口。open -这是一个你打开的窗口,它处理传递的参数并将它们写入SQL数据库,然后关闭自己。这将使原始站点保持完整,但使用您的更改更新数据库。

所以window.open (saveChanges.php ? param1 = . ., param2 = . .,等…)

saveChanges.php文件应该读入传递的参数,并运行适当的SQL来保存更新。

完成后,saveChanges.php文件应该使用window.close()关闭自己

您的数据库现在应该反映您的网页条目的内容。