drazvzi741287 2018-02-13 06:55
浏览 53

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

I am new in php. I trying to know Multiple Inline Insert into Mysql using Ajax JQuery in PHP. Then I follow this tutorial on "webslesson". My all codes and database are Ok as like as webslesson web tutorial . But my save button doesn't work and don't send any data in my database and also not shown any error....
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;
  ?>

This is my database...... 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大