Data does not go into the database,What's the problem?

When I select multiple rows and click create button into database one of the rows decreases,
What’s the problem with javascript and php, because there’s no problem without javascript

As the picture shows, it’s one row insert to db table
2021-04-27_145503

it is full code

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
<?php include_once 'class.token.php'; ?>
  <?php
    $marks_percentage = 0;
    $avarage = 0;

    if(isset($_POST["create_invoice"])){
      if(Token::check($_POST['token']) AND !empty($_POST['token'])){

        $student_id=@output($_POST['student_id']);
        $class_id=@output($_POST['class_id']);

        $stm = $db->prepare("SELECT * FROM results WHERE student_id=:student_id AND class_id=:class_id");
        $stm->bindParam(":student_id", $student_id, PDO::PARAM_STR);
        $stm->bindParam(":class_id", $class_id, PDO::PARAM_STR);
        $stm->execute();
        $rowCount = $stm->rowCount();
        if($rowCount == 0){

       $statement = $db->prepare(" INSERT INTO results (id, student_id, date_created, marks_percentage,avarage,class_id) VALUES (NULL, :student_id, :date_created, :marks_percentage, :avarage, :class_id)");
       $statement->bindParam(":student_id", output($_POST["student_id"]), PDO::PARAM_STR);
       $statement->bindParam(":date_created", output($_POST["date_created"]), PDO::PARAM_STR);
       $statement->bindParam(":marks_percentage", $marks_percentage, PDO::PARAM_STR);
       $statement->bindParam(":avarage", $avarage, PDO::PARAM_STR);
       $statement->bindParam(":class_id", output($_POST["class_id"]), PDO::PARAM_STR);
       $statement->execute();

        $statement = $db->query("SELECT LAST_INSERT_ID()");
        $result_id = $statement->fetchColumn();

        for($count=0; $count<$_POST["total_item"]; $count++)
        {

          $marks_percentage = $marks_percentage + floatval(output($_POST["mark"][$count]));
          $avarage = $avarage + floatval(output($_POST["mark"][$count])) / $_POST["total_item"];

          $statement = $db->prepare("INSERT INTO result_items (id,result_id,subject_id,mark) VALUES (NULL, :result_id, :subject_id, :mark)");
          $statement->bindParam(":result_id", $result_id, PDO::PARAM_STR);
          $statement->bindParam(":subject_id", $_POST["subject_id"][$count], PDO::PARAM_STR);
          $statement->bindParam(":mark", $_POST["mark"][$count], PDO::PARAM_STR);
          $statement->execute();
        }
        $statement = $db->prepare("UPDATE results SET marks_percentage = :marks_percentage,avarage = :avarage WHERE id = :result_id");
        $statement->bindParam(":marks_percentage", $marks_percentage, PDO::PARAM_STR);
        $statement->bindParam(":avarage", $avarage, PDO::PARAM_STR);
        $statement->bindParam(":result_id", $result_id, PDO::PARAM_STR);
        $statement->execute();

        echo "add";
        $db = null;
        //direct("./new_result.php?add");

      }else {
        re("this name have ni the list");
      }
      }else {
        re("try agine");
      }
    }
    //create
    ?>
      <div class="container-fluid">
        <?php
        // zayadkrdn
        if(isset($_GET["add"]))
        {
        ?>
        <form method="post" id="invoice_form">
        <div class="row">

          <div class="col-lg-4">
              <div class="card shadow mb-4 text-right">
                  <div class="card-body">
                   <div class="d-flex flex-column align-items-center text-center p-3 py-4">
                     <img class="rounded-circle mt-3" width="150px" src="https://st3.depositphotos.com/15648834/17930/v/600/depositphotos_179308454-stock-illustration-unknown-person-silhouette-glasses-profile.jpg">
                     <div class="form-group">
                       <label for="" class="control-label">full name</label>
                       <select name="student_id" id="student_id" class="form-control select3 select2-sm">
                         <option></option>
                         <?php echo student($conn); ?>
                       </select>
                        <span>  <small id="class"><?php echo isset($class) ? "Current Class: ".$class : "" ?></small></span>
                       <input type="hidden" name="class_id" value="<?php echo isset($class_id) ? $class_id: '' ?>">
                     </div>
                   </div>
                  </div>
              </div>
          </div>

          <div class="col-lg-8">
              <!-- Basic Card Example -->
              <div class="card shadow mb-4 text-right">
                  <div class="card-header py-3">
                      <h6 class="m-0 font-weight-bold text-gray-800">new exam</h6>
                  </div>
                  <div class="card-body">
                      <div class="form-group">
                        <input type="hidden" name="date_created" class="form-control" id="date_created" value="<?php echo isset($_POST["date_created"]) ? $_POST["date_created"] : '' ?>" placeholder="بەرواری تۆماربوون" readonly>
                      </div>
                    <p align="center"> <button  class="btn btn-block btn-primary" type="button" id="add_row"><i class="fas fa-fw fa-plus"></i></button></p>
                    <div class="table-responsive">
                      <table id="invoice-item-table" class="table table-bordered">
                        <thead>
                          <tr>
                            <th width="15%">#</th>
                            <th width="34%">lesson</th>
                            <th width="25%">mark</th>
                            <th>remove</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td><span id="sr_no">1</span></td>
                            <td>
                              <select name="subject_id[]" id="subject_id1" class="form-control select3 select2-sm input-sm">
                                <option></option>
                                <?php echo subject2($conn); ?>
                              </select>
                            </td>
                            <td><input type="text" name="mark[]" id="mark1" data-srno="1" class="form-control input-sm mark" /></td>
                            <td></td>
                          </tr>
                        </tbody>
                      </table>
                      <div align="right">
                      </div>
                    </div>
                  </div>
                  <div class="card-footer">
                    <div class="d-flex w-100 justify-content-center align-items-center">
                        <input type="hidden" name="total_item" id="total_item" value="1" />
                        <input type="hidden" name="token" value="<?php echo Token::create(); ?>">
                        <p align="center"><input type="submit" name="create_invoice" id="create_invoice" class="btn btn-primary" value="create" /></p>
                    </div>
                  </div>
              </div>
          </div>
      </div>
      </form>
      <script>
      $(document).ready(function() {
      var final_total_amt = $('#final_total_amt').text();
      var options = $('[name="subject_id[]"]').html()
      $('[name="subject_id[]"]').select2()

      function getInvoiceRows() {
        var invoice_item_table = $("#invoice-item-table");
        var rows = invoice_item_table.find("tbody").children();
        return rows;
      }
      function getItemCount() {
        var rows = getInvoiceRows();
        var count = rows.length;
        return count;
      }
      function hasDuplicateItems() {
        var rows = getInvoiceRows();
        var itemNames = rows.find("select").map(function (i, select) {
          return select.value;
        });
        return new Set(itemNames).size < itemNames.length;
      }

      $(document).on("change", "#invoice-item-table select", function(evt) {
        if (hasDuplicateItems()) {
          alert("Please Change Duplicate Item");
          evt.preventDefault();
          return false;
        }
      });
      $(document).on('click', '#add_row', function(evt) {
        if (hasDuplicateItems()) {
          alert("Please Remove Duplicate Items");
          evt.preventDefault();
          return false;
        }

        var count = getItemCount();
        $('#total_item').val(count);
        var html_code = '';
        html_code += '<tr>';
        html_code += '<td><span id="sr_no">' + count + '</span></td>';
        html_code += '<td><select name="subject_id[]" class="form-control select3"><option value="">choce</option>' + options + '</select></td>';
        html_code += '<td><input type="text" name="order_item_quantity[]" data-srno="' + count + '" class="form-control input-sm number_only order_item_quantity" /></td>';
        html_code += '<td><button type="button" name="remove_row" class="btn btn-danger btn-xs remove_row">X</button></td>';
        html_code += '</tr>';

        var $row = $(html_code);
        $row.find('select').select2();
        $('#invoice-item-table').append($row);
      });

      $(document).on('click', '.remove_row', function() {
        var removeButton = this;
        var row = $(removeButton).closest("tr");
        var tbody = row.parent();
        var total_item_amount = row.find(".order_item_final_amount").val();
        var final_amount = $('#final_total_amt').text();
        var result_amount = parseFloat(final_amount) - parseFloat(total_item_amount);
        $('#final_total_amt').text(result_amount);
        row.remove();
        $('#total_item').val(tbody.children().length);
      });

      $('#create_invoice').click(function(evt) {

        if ($.trim($('#student_id').val()).length == 0) {
          alert("Please Enter Invoice Number");
          return false;
        }


        if (hasDuplicateItems()) {
          alert("Please Remove Duplicate Items");
          evt.preventDefault();
          return false;
        }

        var invoice_item_table = $("#invoice-item-table");
        var rows = getInvoiceRows();
        for (i = 0; i < rows.length; i += 1) {
          var itemRow = rows[i];
          var itemName = $(itemRow).find("select");
          if ($.trim(itemName.val()).length == 0) {
            alert("Please Enter Item Name");
            itemName.focus();
            evt.preventDefault();
            return;
          }


        }
      });
      });

        </script>
        <?php
        }
        ?>
</div>
<?php include_once 'footer.php'; ?>

Sorry, i wrote this subject before in javascript part but the problem was not solved

What is in $_POST when you submit the form?

s’an awful lot of strings for things that probably arent supposed to be strings… just sayin.

As I mentioned, the exact number of rows does not go into databases

I removed them, but It wasn’t a solution.

The most immediate problem is the ‘#’ total_item/count, e.g. the ‘1’ and ‘1’ column values, being used when you dynamically add a row to the form, is the starting value, not the value after the row as been added. You are only telling the php code to loop one time when there are two rows of data. I recommend that you simply write the php code so that it loops over the submitted arrays of subject_id/mark data and forget about the whole total_item/count code.

A second problem is that the dynamically added field name for the ‘mark’ value is actually - name="order_item_quantity[]"