如何在单个数据库列中插入动态添加的输入字段值?

时间:2019-06-03 05:09:51

标签: javascript php jquery

我在应用程序中添加了2个动态添加的输入字段。

我想在2个数据库列中插入2个字段值。

<div class="form-group ">
    <div id="itemRows"  class="col-md-12">
       <div class="row">
          <label>Earnings</label> <input type="text" name="add_qty" size="4" /> 
          <label>Amount <input type="text" name="add_name"   oninput="this.value 
           = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" /> 
           <button onclick="addRow(this.form);"><i class="menu-icon mdi mdi-
           plus-circle"></i></button></div>

           <script>
           var rowNum = 0;
           function addRow(frm) {
            rowNum ++;
            var row = '<p id="rowNum'+rowNum+'">Earnings: <input type="text" 
             name="qty[]" size="4" value="'+frm.add_qty.value+'"> Amount: <input 
             type="text"   name="name[]"   value="'+frm.add_name.value+'"> 
             <input type="button" value="Remove" 
              onclick="removeRow('+rowNum+');"></p>';
              jQuery('#itemRows').append(row);
               frm.add_qty.value = '';
               frm.add_name.value = '';
               }

            </script>
            <script>
                 function removeRow(rnum) {
                    jQuery('#rowNum'+rnum).remove();
                 }
            </script>
       </div>
</div>

我要在数据库的收入列和金额列中添加所有收入

1 个答案:

答案 0 :(得分:0)

首先请更正您的代码。在这种命名方式中,两个HTML都不同

<?php include 'config.php';

for($i = 0; $i < count($_POST['add_qty']); $i++)
{
    $add_qty = mysqli_real_escape_string($connect, $_POST['add_qty'][$i]);
    $add_name = mysqli_real_escape_string($connect, $_POST['add_name'][$i]);

    if (empty(trim($add_qty))) continue;

    $sql = "INSERT INTO earnigs_tbl(earnings, amount)
            VALUES('$add_qty', '$add_name')";
    mysqli_query($connect, $sql);
}

if(mysqli_error($connect))
{
    echo "Data base error occure";
}
else
{
    echo $i . " rows added";
}
?>
<div class="form-group ">
    <div id="itemRows"  class="col-md-12">
       <div class="row">
          <label>Earnings</label> <input type="text" name="add_qty" size="4" /> 
          <label>Amount <input type="text" name="add_name"   oninput="this.value 
           = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" /> 
           <button onclick="addRow(this.form);"><i class="menu-icon mdi mdi-
           plus-circle"></i></button></div>

           <script>
           var rowNum = 0;
           function addRow(frm) {
            rowNum ++;
            var row = '<p id="rowNum'+rowNum+'">Earnings: <input type="text" 
             name="add_qty[]" size="4" value="'+frm.add_qty.value+'"> Amount: <input 
             type="text"   name="add_name[]"   value="'+frm.add_name.value+'"> 
             <input type="button" value="Remove" 
              onclick="removeRow('+rowNum+');"></p>';
              jQuery('#itemRows').append(row);
               frm.add_qty.value = '';
               frm.add_name.value = '';
               }

            </script>
            <script>
                 function removeRow(rnum) {
                    jQuery('#rowNum'+rnum).remove();
                 }
            </script>
       </div>
</div>