Javascript 将最大值+1插入数据库中的新值

Javascript 将最大值+1插入数据库中的新值,javascript,php,html,sql-server,ajax,Javascript,Php,Html,Sql Server,Ajax,我被这件事缠住了,所以我非常感谢你的帮助。我目前有一个按钮,可以按下它,弹出一个HTML弹出框。弹出框有两个输入字段,SKU Group和Group_ID。我希望Group_ID在实际插入查询运行之前从数据库中提取MAX+1值 最好的方法是什么?如何在代码中有效地实现它 我当前的HTML: <form> <fieldset> <label for="sku_group">SKU Group</label>

我被这件事缠住了,所以我非常感谢你的帮助。我目前有一个按钮,可以按下它,弹出一个HTML弹出框。弹出框有两个输入字段,SKU Group和Group_ID。我希望Group_ID在实际插入查询运行之前从数据库中提取MAX+1值

最好的方法是什么?如何在代码中有效地实现它

我当前的HTML:

<form>
    <fieldset>        

      <label for="sku_group">SKU Group</label>
      <input type="text" name="group" id="group" class="text ui-widget-content ui-corner-all">
      <label for="group_id">Group_ID</label>
      <input type="text" name="id" id="id" class="text ui-widget-content ui-corner-all">


      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>

为什么不使用标识列或序列?这正是他们的目的。您可以使用MAX+1,但它的伸缩性不好,需要比您使用的更严格的隔离语义。好的,我想知道这是否是解决方案,但不确定…我将继续尝试此路线这是我当前加载表的查询…按LengroupID从SKU\u Group\u Dim ORDER选择*,Group_ID那么我如何插入我的标识列,使其按照我上面的查询的当前顺序插入,从而使值正确对齐?为什么需要按值的长度顺序分配无意义的标识列?我的头脑一片空白。我不知道为什么我认为我需要这个。。
<?php

  $SKU_Group = $_POST['SKU_Group'];
  $Group_ID = $_POST['Group_ID'];

  $host="xxxxxxx"; 
  $dbName="xxxx"; 
  $dbUser="xxxxxxxxxx"; 
  $dbPass="xxxxxxxx";

  $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);

  //$max = "SELECT MAX(Group_ID) FROM SKU_Group_Dim";

  $sql = "INSERT INTO SKU_Group_Dim (Group_ID, [SKU Group]) SELECT MAX (Group_ID) + 1, ? FROM SKU_Group_Dim";

  $stmt = $pdo->prepare($sql);
  $result = $stmt->execute(array($SKU_Group, $Group_ID));
  echo json_encode($result);

  }

?>
// ----- Dialog Box for adding a row -----

$( function() {   

    var dialog, form,

      sku_group = $( "#group" ),
      group_id = $( "#id" ),
      allFields = $( [] ).add( sku_group ).add( group_id ),
      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 addGroup() {

      var valid = true;
      allFields.removeClass( "ui-state-error" );
// ----- Validation for each input in add row dialog box -----
      //valid = valid && checkRegexp( sku_group, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid SKU Group name" );
      //valid = valid && checkRegexp( group_id, /^[a-z]([0-9a-z_\s])+$/i, "Please enter a valid SKU Group name" );
      console.log(allFields);
      if ( valid ) {
        var $tr = $( "#skuTable tbody tr td" ).eq(0).clone();
        var dict = {};
        var errors = "";
        $tr.each(function(){
        var type = $(this).attr('id');
        var value = $(this).html();
        console.log(type + " : " + value);

      switch (type) {
        case "group":
            dict["SKU Group"] = value;
        break;
        case "id":
            dict["Group_ID"] = value;
        break;
        }
    });
        $( "#skuTable tbody" ).append($tr);
        dialog.dialog( "close" );
        console.log(dict);


        var request = $.ajax({
          type: "POST",
          url: "insert-group.php",
          data:{ 'SKU_Group' : $('#group').val(), 'Group_ID' : $('#id').val() }
        });

        request.done(function (response, textStatus, jqXHR){
          if(JSON.parse(response) == true){
            console.log("row inserted");
          } else {
            console.log("row failed to insert");
            console.log(response);
          }
        });

        // Callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown){
            console.error(
                "The following error occurred: "+
                textStatus, errorThrown
            );
        });

        // Callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {

        });

}

      return valid;
    }

    var dialog = $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 400,
      width: 350,
      modal: true,
      buttons: {
        "Add Group": addGroup,
        Cancel: function() {
          dialog.dialog( "close" );
        }
      },
      close: function() {
        form[ 0 ].reset();
      }
    });

    form = dialog.find( "form" ).on( "submit", function( event ) {
      event.preventDefault();
      addGroup();
    });

    $( ".create-user" ).button().on( "click", function() {
      dialog.dialog({
          show: 'blind',
          hide: 'blind'
      });
      dialog.dialog("open");
    });

  });