将MAX +1插入数据库中的新值

时间:2017-02-20 14:55:37

标签: javascript php html sql-server ajax

我非常坚持这一点,所以你的帮助将不胜感激。我目前有一个按钮可以按下,弹出一个HTML弹出框。弹出框有2个输入字段,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>

嵌件group.php:

<?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);

  }

?>

用于添加行的JavaScript:

// ----- 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");
    });

  });

0 个答案:

没有答案