在HTML表中添加行时,ID为NULL并且不会自动递增

时间:2016-12-01 18:54:49

标签: javascript php html sql-server

我有一个包含7个列的HTML表,其中包含一个插入按钮。只要按下插入按钮,就会出现一个对话框,您可以输入信息。添加行后,我希望它自动将MR_ID列分配给MAX MR_ID为+1。因此,例如,如果最大MR_ID是300并且我添加了一行,我希望添加的行的MR_ID为301,因此基本上就像自动增量。目前,每当我添加一行时,MR_ID在表中显示为0,并且在数据库中为NULL。 MR_ID是对其他表的查找,因此我不能将其设置为NULL或与另一行相同的数字。我怎么能这样做?

我知道这是很多代码但是如果你有任何问题,我会在这里帮助你让我的代码更清楚,如果可以的话。

表格的HTML / PHP:

<?php
    /* Foreach loop that brings in information to populate table */
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr id="<?php echo intval ($rows['MR_ID'])?>">
        <td class="mr_id" id="<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td class="mr_name" id="mr_name-<?php echo intval ($rows['MR_ID'])?>" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td>
        <td class="buyer_id" id="buy_id<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
        <td class="poc_n" id="poc_n-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>     
        <td class="poc_e" id="poc_e-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
        <td class="poc_p" id="poc_p-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
        <td><input type="button" class="edit" name="edit" value="Edit">
    </tr>
 <?php
  }
 ?>

JavaScript的:

function addVendor() {
      var valid = true;
      allFields.removeClass( "ui-state-error" );
// ----- Validation for each input in add row dialog box -----
      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" );
      console.log(allFields);
      if ( valid ) {
        var $tr = $( "#html_master tbody tr" ).eq(0).clone();
        var dict = {};
        var errors = "";
        $.each(allFields, function(){
          $tr.find('.' + $(this).attr('id')).html( $(this).val()+"-"+buyer_id );
          var type = $(this).attr('id');
          var value = $(this).val();
          console.log(type + " : " + value);
          // ----- Switch statement that provides validation for each table cell -----
          switch (type) {
             case "mr_id":
                dict["MR_ID"] = value;
              break;
            case "mr_name":
                dict["MR_Name"] = value;
              break;
            case "buyer_id":
                dict["Buyer_ID"] = value;
              break;
            case "poc_n":
                dict["MR_POC_N"] = value;
              break;
            case "poc_e":
                dict["MR_POC_E"] = value;
                break;
            case "poc_p":
                dict["MR_POC_P"] = value;
                break;
            }
        });
        console.log(dict);
        $tr.find('.mr_id').html( $( "#html_master tbody tr" ).length + 1 );
        $( "#html_master tbody" ).append($tr);
        dialog.dialog( "close" );


        var request = $.ajax({
          type: "POST",
          url: "insert-copy.php",
          data: dict
        });

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

Ajax插入脚本:

<?php

  $MR_ID = $_POST['MR_ID'];
  $MR_Name = $_POST['MR_Name'];
  $Buyer_ID = $_POST['Buyer_ID'];
  $MR_POC_N = $_POST['MR_POC_N'];
  $MR_POC_E = $_POST['MR_POC_E'];
  $MR_POC_P = $_POST['MR_POC_P'];

  $host="xxxxxxxxxx"; 
  $dbName="xxxxxx"; 
  $dbUser="xxxxxxxxxxxxx"; 
  $dbPass="xxxxxxxxxx";

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

  $sql = "INSERT INTO Stage_Rebate_Master (MR_ID, MR_Name, Buyer_ID, MR_POC_N, MR_POC_E, MR_POC_P) VALUES (?, ?, ?, ?, ?, ?)";
  $stmt = $pdo->prepare($sql);
  $result = $stmt->execute(array($MR_ID, $MR_Name, $Buyer_ID, $MR_POC_N, $MR_POC_E, $MR_POC_P));
  echo json_encode($result);

?>

0 个答案:

没有答案