使用PHP中的Ajax JQuery将多个内联插入到Mysql中

时间:2018-02-13 06:55:47

标签: php jquery mysql ajax

我是php新手。我试图在PHP中使用Ajax JQuery知道多个内联插入到Mysql中。 然后我按照“webslesson”这个教程。我的所有代码和数据库都像Weblesson网络教程一样好。但我的保存按钮不起作用,不发送我的数据库中的任何数据,也没有显示任何错误.... 的index.php

<!DOCTYPE html>
<html>
<head>
<title>Multiple Inline Insert into Mysql using Ajax JQuery in PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

 </head>
 <body>
 <br /><br />
 <div class="container">
 <br />
 <h2 align="center">Multiple Inline Insert into Mysql using Ajax JQuery in PHP</h2>
 <br />
 <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="button" name="save" id="save" class="btn btn-info">Save</button>
  </div>
  <br />
  <div id="inserted_item_data"></div>
  </div>

  </div>
 </body>
</html>

<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();
}
fetch_item_data();
}
 });
 });

 function fetch_item_data()
  {
 $.ajax({
 url:"fetch.php",
 method:"POST",
 success:function(data)
 {
  $('#inserted_item_data').html(data);
 }
  })
  }
  fetch_item_data();

   });
  </script>

insert.php

<?php
//insert.php
$connect = mysqli_connect("127.0.0.1", "root", "", "testing4");
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($connect, $item_name[$count]);
     $item_code_clean = mysqli_real_escape_string($connect, $item_code[$count]);
     $item_desc_clean = mysqli_real_escape_string($connect, $item_desc[$count]);
     $item_price_clean = mysqli_real_escape_string($connect, $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($connect, $query))
     {
    echo 'Item Data Inserted';
     }
    else
    {
      echo 'Error';
    }
   }
     else
   {
     echo 'All Fields are Required';
   }
   }
   ?>

fetch.php

 <?php
//fetch.php
 $connect = mysqli_connect("127.0.0.1", "root", "", "testing4");
 $output = '';
 $query = "SELECT * FROM item ORDER BY item_id DESC";
 $result = mysqli_query($connect, $query);
 $output = '
 <br />
 <h3 align="center">Item Data</h3>
 <table class="table table-bordered table-striped">
  <tr>
  <th width="30%">Item Name</th>
  <th width="10%">Item Code</th>
  <th width="50%">Description</th>
  <th width="10%">Price</th>
  </tr>
   ';
   while($row = mysqli_fetch_array($result))
   {
    $output .= '
    <tr>
  <td>'.$row["item_name"].'</td>
  <td>'.$row["item_code"].'</td>
  <td>'.$row["item_description"].'</td>
  <td>'.$row["item_price"].'</td>
  </tr>
   ';
    }
  $output .= '</table>';
  echo $output;
  ?>

这是我的数据库...... item.sql

--
-- Database: `testing4`
- -

-- --------------------------------------------------------

--
-- Table structure for table `item`
--

CREATE TABLE `item` (
   `item_id` int(11) NOT NULL,
    `item_name` varchar(250) NOT NULL,
    `item_code` varchar(250) NOT NULL,
    `item_description` text NOT NULL,
    `item_price` varchar(30) NOT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

0 个答案:

没有答案