jquery 动态发票表数据未正确插入

bvpmtnay  于 2023-03-29  发布在  jQuery
关注(0)|答案(1)|浏览(123)

我正在开发发票系统的动态表。添加和删除行动态工作完美。但只有第一行数据插入成功的mysql表。但其余的行不插入。我找不到问题。我认为**$(“#add_row”).click(function()**是主要问题。你能帮助我解决这个问题吗?

<?php

  $conn = mysqli_connect("localhost", "root", "", "dynamic");

  if (isset($_POST["addInvoice"]))   {
    $customerName = $_POST["customerName"];

    $sql = "INSERT INTO invoices (customerName) VALUES ('$customerName')";
    mysqli_query($conn, $sql);
    $invoiceId = mysqli_insert_id($conn);

    for ($a = 0; $a < count($_POST["product"]); $a++)
    {
      $sql = "INSERT INTO items (invoiceId,itemName,itemdescription,itemqty,itemrate,itemAmount) VALUES ('$invoiceId', '" . $_POST["product"][$a] . "', '" . $_POST["description"][$a] . "'
        ,'" . $_POST["qty"][$a] . "','" . $_POST["price"][$a] . "','" . $_POST["total"][$a] . "')";

      mysqli_query($conn, $sql);
    }

    echo "<p>Invoice has been added.</p>";   }

?>

<div class="container">   <div class="row clearfix">
    <div class="col-md-12">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr>
            <th class="text-center"> # </th>
            <th class="text-center"> Product </th>
            <th class="text-center"> Desciption </th>
            <th class="text-center"> Qty </th>
            <th class="text-center"> Price </th>
            <th class="text-center"> Total </th>
          </tr>
        </thead>
         <form method="POST" action="">

  <input type="text" name="customerName" placeholder="Enter customer name" required>

        <tbody>
          <tr id='addr0'>
            <td>1</td>
            <td><input type="text" name='product[]'  placeholder='Enter Product Name' class="form-control"/></td>

            <td><input type="text" name='description[]'  placeholder='Enter Description Name' class="form-control"/></td>

            <td><input type="text" name='qty[]' placeholder='Enter Qty' class="form-control qty" step="0" min="0"/></td>
            <td><input type="text" name='price[]' placeholder='Enter Unit Price' class="form-control price" step="0.00" min="0"/></td>
            <td><input type="text" name='total[]' placeholder='0.00' class="form-control total" readonly/></td>
          </tr>
          <tr id='addr1'></tr>
        </tbody>
      </table>
    </div>   </div>

  <div class="row clearfix" style="margin-top:20px">
    <div class="pull-right col-md-4">
      <table class="table table-bordered table-hover" id="tab_logic_total">
        <tbody>
          <tr>
            <th class="text-center">Sub Total</th>
            <td class="text-center"><input type="number" name='sub_total' placeholder='0.00' class="form-control" id="sub_total" readonly/></td>
          </tr>
          <tr>
            <th class="text-center">Tax</th>
            <td class="text-center"><div class="input-group mb-2 mb-sm-0">
                <input type="number" class="form-control" id="tax" placeholder="0">
                <div class="input-group-addon">%</div>
              </div></td>
          </tr>
          <tr>
            <th class="text-center">Tax Amount</th>
            <td class="text-center"><input type="number" name='tax_amount' id="tax_amount" placeholder='0.00' class="form-control" readonly/></td>
          </tr>
          <tr>
            <th class="text-center">Grand Total</th>
            <td class="text-center"><input type="number" name='total_amount' id="total_amount" placeholder='0.00' class="form-control" readonly/></td>
          </tr>
        </tbody>   <input type="submit" name="addInvoice" value="Add Invoice">   </form>   <div class="row clearfix">
    <div class="col-md-12">
      <button id="add_row" class="btn btn-default pull-left">Add Row</button>
      <button id='delete_row' class="pull-right btn btn-default">Delete Row</button>
    </div>   </div>
      </table>
    </div>   </div> </div>

Jquery脚本

$(document).ready(function(){
    var i=1;
    $("#add_row").click(function()

    {
        b=i-1;
        $('#addr'+i).html($('#addr'+b).html()).find('td:first-child').html(i+1);
        $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
        i++; 
    });

    $("#delete_row").click(function(){
        if(i>1){
        $("#addr"+(i-1)).html('');
        i--;
        }
        calc();
    });
    
    $('#tab_logic tbody').on('keyup change',function(){
        calc();
    });
    $('#tax').on('keyup change',function(){
        calc_total();
    });
    

});

function calc()
{
    $('#tab_logic tbody tr').each(function(i, element) {
        var html = $(this).html();
        if(html!='')
        {
            var qty = $(this).find('.qty').val();
            var price = $(this).find('.price').val();
            $(this).find('.total').val(qty*price);
            
            calc_total();
        }
    });
}

function calc_total()
{
    total=0;
    $('.total').each(function() {
        total += parseInt($(this).val());
    });
    $('#sub_total').val(total.toFixed(2));
    tax_sum=total/100*$('#tax').val();
    $('#tax_amount').val(tax_sum.toFixed(2));
    $('#total_amount').val((tax_sum+total).toFixed(2));
}
anauzrmj

anauzrmj1#

这是正确的编码

<?php
    
      $conn = mysqli_connect("localhost", "root", "", "dynamic");
    
      if (isset($_POST["addInvoice"]))
      {
        $customerName = $_POST["customerName"];
    
        $sql = "INSERT INTO invoices (customerName) VALUES ('$customerName')";
        mysqli_query($conn, $sql);
        $invoiceId = mysqli_insert_id($conn);
    
        for ($a = 0; $a < count($_POST["product"]); $a++)
        {
          $sql = "INSERT INTO items (invoiceId,itemName,itemdescription,itemqty,itemrate,itemAmount) VALUES ('$invoiceId', '" . $_POST["product"][$a] . "', '" . $_POST["description"][$a] . "'
            ,'" . $_POST["qty"][$a] . "','" . $_POST["price"][$a] . "','" . $_POST["total"][$a] . "')";
    
          mysqli_query($conn, $sql);
        }
         echo mysqli_error($conn) . "\n";
        echo "<p>Invoice has been added.</p>";
      }
    
    ?>
    
    <form method="POST" action="">
    
    <div class="container">
      <div class="row clearfix">
        <div class="col-md-12">
          <table class="table table-bordered table-hover" id="tab_logic">
            <thead>
              <tr>
                <th class="text-center"> # </th>
                <th class="text-center"> Product </th>
                <th class="text-center"> Desciption </th>
                <th class="text-center"> Qty </th>
                <th class="text-center"> Price </th>
                <th class="text-center"> Total </th>
              </tr>
            </thead>
            
    
    
      <input type="text" name="customerName" placeholder="Enter customer name" required>
    
            <tbody>
              <tr id='addr0'>
                <td>1</td>
                <td><input type="text" name='product[]'  placeholder='Enter Product Name' class="form-control"/></td>
    
                <td><input type="text" name='description[]'  placeholder='Enter Description Name' class="form-control"/></td>
    
                <td><input type="text" name='qty[]' placeholder='Enter Qty' class="form-control qty" step="0" min="0"/></td>
                <td><input type="text" name='price[]' placeholder='Enter Unit Price' class="form-control price" step="0.00" min="0"/></td>
                <td><input type="text" name='total[]' placeholder='0.00' class="form-control total" readonly/></td>
              </tr>
              <tr id='addr1'></tr>
            </tbody>
          </table>
        </div>
      </div>
    
      <div class="row clearfix" style="margin-top:20px">
        <div class="pull-right col-md-4">
          <table class="table table-bordered table-hover" id="tab_logic_total">
            <tbody>
              <tr>
                <th class="text-center">Sub Total</th>
                <td class="text-center"><input type="number" name='sub_total' placeholder='0.00' class="form-control" id="sub_total" readonly/></td>
              </tr>
              <tr>
                <th class="text-center">Tax</th>
                <td class="text-center"><div class="input-group mb-2 mb-sm-0">
                    <input type="number" class="form-control" id="tax" placeholder="0">
                    <div class="input-group-addon">%</div>
                  </div></td>
              </tr>
              <tr>
                <th class="text-center">Tax Amount</th>
                <td class="text-center"><input type="number" name='tax_amount' id="tax_amount" placeholder='0.00' class="form-control" readonly/></td>
              </tr>
              <tr>
                <th class="text-center">Grand Total</th>
                <td class="text-center"><input type="number" name='total_amount' id="total_amount" placeholder='0.00' class="form-control" readonly/></td>
              </tr>
            </tbody>
      <input type="submit" name="addInvoice" value="Add Invoice">
    
      <div class="row clearfix">
        <div class="col-md-12">
          <button id="add_row" class="btn btn-default pull-left">Add Row</button>
          <button id='delete_row' class="pull-right btn btn-default">Delete Row</button>
        </div>
      </div>
          </table>
        </div>
      </div>
    </div>
    
      </form>

相关问题