2017-07-17 73 views
0

輸入動態字段到mysql數據庫失敗,但我不明白爲什麼。 html和php文件都給出了,請幫我修改代碼並提供關於如何執行代碼的想法。輸入動態字段到mysql數據庫失敗

<html> 
     <head> 
      <title>Dynamically Add or Remove input fields in PHP with JQuery</title> 
      <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.6/js/bootstrap.min.js"></script> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
     </head> 
     <body> 
      <div class="container"> 
       <br /> 
       <br /> 
       <h2 align="center">Purchase Entry</h2> 
       <div class="form-group"> 
        <form name="add_name" id="add_name"> 
          <div class="table-responsive"> 
           <table class="table table-bordered" id="dynamic_field"> 
            <tr> 
             <td><input type="text" name="name[]" placeholder="Invoice No." class="form-control name_list" /></td> 
             <td><input type="text" name="name[0]" placeholder="Description" class="form-control name_list" /></td> 
             <td><input type="text" name="name[1]" placeholder="Unit" class="form-control name_list" /></td> 
             <td><input type="text" name="name[2]" placeholder="Quantity" class="form-control name_list" /></td> 
             <td><input type="text" name="name[3]" placeholder="Amount" class="form-control name_list" /></td> 
             <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td> 
            </tr> 
           </table> 
           <input type="button" name="submit" id="submit" class="btn btn-info" value="Add and Save" /> 
          </div> 
        </form> 
       </div> 
      </div> 
     </body> </html> <script> $(document).ready(function(){ 
     var i=1; 
     $('#add').click(function(){ 
      i++; 
      $('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" placeholder="Invoice No." class="form-control name_list" /></td><td><input type="text" name="name[0]" placeholder="Description" class="form-control name_list" /></td><td><input type="text" name="name[1]" placeholder="Unit" class="form-control name_list" /></td></td><td><input type="text" name="name[2]" placeholder="Quantity" class="form-control name_list" /></td><td><input type="text" name="name[3]" placeholder="Amount" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>'); 
     }); 
     $(document).on('click', '.btn_remove', function(){ 
      var button_id = $(this).attr("id"); 
      $('#row'+button_id+'').remove(); 
     }); 
     $('#submit').click(function(){    
      $.ajax({ 
       url:"purchasesave.php", 
       method:"POST", 
       data:$('#add_name').serialize(), 
       success:function(data) 
       { 
        alert(data); 
        $('#add_name')[0].reset(); 
       } 
      }); 
     }); }); </script> 

和PHP文件是這樣的,但我想添加的每個字段的mysql

<?php 
include "includes/header.php"; 
include "includes/mydb.php"; 
include "includes/veriuser.php"; 

$conn = new mysqli($servername,$username,$password,$database) 
?> 
<?php 
$number = count($_POST["purchase"]); 
if($number > 0) 
{ 
     for($i=0; $i<$number; $i++) 
     { 
      if(trim($_POST["purchase"][$i] != '')) 
      { 
       $sql = "INSERT INTO tbl_name(purchase) VALUES('".mysqli_real_escape_string($conn, $_POST["name"][$i])."')"; 
       mysqli_query($conn, $sql); 
      } 
     } 
     echo "Data Inserted"; 
} 
else 
{ 
     echo "Please Enter Name"; 
} 
?> 
+0

看看你的ajax請求。寫下來就是這樣的:'data:{'purchase':$('#add_name')。serialize()},' – Richard

+0

請更正php顯示的整個彈出代碼 –

回答

0

我發現了需要更新的問題

$.ajax({ 
       url:"purchasesave.php", 
       method:"POST", 
       data:{ 'purchase' : $('#add_name').serialize() }, 
       success:function(data) 
       { 
        alert(data); 
        $('#add_name')[0].reset(); 
       } 
      }); 

這是常見的格式,你使用$number = count($_POST["purchase"]);,所以你需要在你的ajax請求中發送這個變量。現在檢查你的ajax請求