简体   繁体   中英

Multiple Inline Insert into table

I am trying to insert in other table using for invoicing

I've tried to create insert function but it is not inserting into table and every time I click the page or even the input tag its always says localhost but empty.

Here is the form:

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

and here is the insert page:

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

}
    }
?>

It must be inserted in different table to call them out in different page.

instead of if(isset($_POST['item_name']))

I tried different approach like this:

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]."');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM