javascript - 多个内联插入到表中

标签 javascript php jquery mysql

我正在尝试插入其他用于开发票的表

我尝试创建插入函数,但它没有插入到表中,每次我单击页面甚至输入标签时,它总是显示 localhost 但为空。

这是表格:

<form class="form-vertical" name="save" id="save" enctype="multipart/form-data" method="post" accept-charset="utf-8">
                <div class="form-group row">
                    <label for="inv_date" >Date</label>
                    <div class="col-sm-8">
                        <input type="date" class="form-control" autocomplete="off"  id="inv_date" name="inv_date" required>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="inv_num" >Inv Number</label>
                    <div class="col-sm-8">
                        <input type="number" class="form-control" autocomplete="off"  id="inv_num" name="inv_num" required>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-bordered" id="crud_table">
                        <tr>
                            <th width="30%">Item Name</th>
                            <th width="10%">Item Code</th>
                            <th width="45%">Description</th>
                            <th width="10%">Price</th>
                            <th width="5%"></th>
                        </tr>

                        <tr>
                            <td contenteditable="true" class="item_name"></td>
                            <td contenteditable="true" class="item_code"></td>
                            <td contenteditable="true" class="item_desc"></td>
                            <td contenteditable="true" class="item_price"></td>
                            <td></td>
                        </tr>
                    </table>
                    <div align="right">
                        <button type="button" name="add" id="add" class="btn btn-success btn-xs">+</button>
                    </div>
                    <div align="center">
                        <button type="submit" name="save" id="save" class="btn btn-info">Save</button>
                    </div>
                </div>
            </form>
        </div>

        <script>
            $(document).ready(function(){
                var count = 1;
                $('#add').click(function(){
                    count = count + 1;
                    var html_code = "<tr id='row"+count+"'>";
                    html_code += "<td contenteditable='true' class='item_name'></td>";
                    html_code += "<td contenteditable='true' class='item_code'></td>";
                    html_code += "<td contenteditable='true' class='item_desc'></td>";
                    html_code += "<td contenteditable='true' class='item_price' ></td>";
                    html_code += "<td><button type='button' name='remove' data-row='row"+count+"' class='btn btn-danger btn-xs remove'>-</button></td>";   
                    html_code += "</tr>";  
                    $('#crud_table').append(html_code);
                });

                $(document).on('click', '.remove', function(){
                    var delete_row = $(this).data("row");
                    $('#' + delete_row).remove();
                });

                $('#save').click(function(){

                    var item_name = [];
                    var item_code = [];
                    var item_desc = [];
                    var item_price = [];

                    $('.item_name').each(function(){
                        item_name.push($(this).text());
                    });
                    $('.item_code').each(function(){
                        item_code.push($(this).text());
                    });
                    $('.item_desc').each(function(){
                        item_desc.push($(this).text());
                    });
                    $('.item_price').each(function(){
                        item_price.push($(this).text());
                    });
                    $.ajax({
                        url:"insert.php",
                        method:"POST",
                        data:{item_name:item_name,item_code:item_code, item_desc:item_desc, item_price:item_price},
                        success:function(data){
                            alert(data);
                            $("td[contentEditable='true']").text("");
                            for(var i=2; i<= count; i++)
                            {
                                $('tr#'+i+'').remove();
                            }

                        }
                    });
                });


            });
        </script>

这是插入页面:

<?php
session_start();
//insert.php
require_once ('connect.php');

if((!empty($_POST['inv_date'])) && (!empty($_POST['inv_num']))){
    $inv_date = $_POST["inv_date"];
    $inv_num = $_POST["inv_num"];
    $query = '';

    $query = 'INSERT INTO invtran (inv_date, inv_num) VALUES ("'.$inv_date.'", "'.$inv_num.'")';
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));


if(isset($_POST["item_name"]))
{

 $item_name = $_POST["item_name"];
 $item_code = $_POST["item_code"];
 $item_desc = $_POST["item_desc"];
 $item_price = $_POST["item_price"];
 $query = '';
 for($count = 0; $count<count($item_name); $count++)
 {
  $item_name_clean = mysqli_real_escape_string($conn, $item_name[$count]);
  $item_code_clean = mysqli_real_escape_string($conn, $item_code[$count]);
  $item_desc_clean = mysqli_real_escape_string($conn, $item_desc[$count]);
  $item_price_clean = mysqli_real_escape_string($conn, $item_price[$count]);
  if($item_name_clean != '' && $item_code_clean != '' && $item_desc_clean != '' && $item_price_clean != '')
  {
   $query .= '
   INSERT INTO item(item_name, item_code, item_description, item_price) 
   VALUES("'.$item_name_clean.'", "'.$item_code_clean.'", "'.$item_desc_clean.'", "'.$item_price_clean.'"); 
   ';
  }
 }
 if($query != '')
 {
  if(mysqli_multi_query($conn, $query))
  {
   echo 'Item Data Inserted';
  }
  else
  {
   echo 'Error';
  }
 }else
 {
  echo 'All Fields are Required';
 }

}
    }
?>

必须插入到不同的表中才能在不同的页面中调用它们。

最佳答案

代替 if(isset($_POST['item_name']))

我试过这样的不同方法:

if ($result) {

    $j = 0;

    $count = sizeof($_POST['po_qty']); // ive use the sizeof() to get the count of the rows

    // Use insert_id property
    $po_trans_id = $link->insert_id;
    $user  = $_SESSION["username"];

    for ($j = 0; $j < $count; $j++) {

      $query = "INSERT INTO request_po (item_name, item_code, item_description, item_price) VALUES (
        '".$item_name[$j]."',
        '".$item_code[$j]."',
        '".$item_description[$j]."',
        '".$item_price[$j]."');

关于javascript - 多个内联插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56143474/

相关文章:

javascript - 如何根据类似的类获取容器内元素的索引?

JavaScript - 从函数返回 false 不起作用?

php - PDO - 获取当前插入的 ID

jquery - Rails - 表单的 jQuery UI 对话框?

javascript - jQuery - 仅适用于 IE 的 css

javascript - 如何将 "string"转换为 "timestamp without time zone"

c# - 使用 JavaScript 连接到 .NET 服务器

javascript - 注释颜色仅在文本位于 Google 条形图之外时显示

javascript - 我应该使用什么命名方案来使用 AJAX 将数据传递到 MySQL 表

php - HTML 模板中的 PSR-2 兼容内联 PHP 标签