尝试从bootstrap模式将数据更新到数据库中

时间:2017-11-29 20:30:25

标签: php jquery ajax

我不能为我的生活弄清楚如何将数据导入数据库。在我的网站的首页上,我有一个已连接到数据库的表,所以基本上我已经建立了一个后台管理页面来更快地输入信息,当它在后面的管理页面输入时,它将更新数据库,然后更新表格在网页的正面。当我尝试在模态中添加信息并且提交页面变为空白时除了添加按钮。当我刷新所有信息都回到桌面上。 table with infotable after sumbit

*麻烦的编码员

  <?php  
 include_once 'core/init.php';
 $query = "SELECT * FROM products ORDER BY id DESC";  
 $result = mysqli_query($conn, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Update Products</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.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.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:700px;">  
                <h3 align="center">Update MySQL Data</h3>  
                <br />  
                <div class="table-responsive">  
                     <div align="right">  
                          <button type="button" name="add" id="add" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-warning">Add</button>  
                     </div>  
                     <br />  
                     <div id="products_table">  
                          <table class="table table-bordered">  
                               <tr>  
                                    <th width="70%">Products Name</th>  
                                    <th width="15%">Edit</th>  
                                    <th width="15%">View</th>  
                               </tr>  
                               <?php  
                               while($row = mysqli_fetch_array($result))  
                               {  
                               ?>  
                               <tr>  
                                    <td><?php echo $row["title"]; ?></td>  
                                    <td><input type="button" name="edit" value="Edit" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs edit_data" /></td>  
                                    <td><input type="button" name="view" value="view" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td>  
                               </tr>  
                               <?php  
                               }  
                               ?>  
                          </table>  
                     </div>  
                </div>  
           </div>  
      </body>  
 </html>  
 <div id="dataModal" class="modal fade">  
      <div class="modal-dialog">  
           <div class="modal-content">  
                <div class="modal-header">  
                     <button type="button" class="close" data-dismiss="modal">&times;</button>  
                     <h4 class="modal-title">Product Details</h4>  
                </div>  
                <div class="modal-body" id="products_detail">  
                </div> 
                <div class="modal-footer">  
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
                </div>  
           </div>  
      </div>  
 </div> 
</div>  
 <div id="add_data_Modal" class="modal fade">  
      <div class="modal-dialog">  
           <div class="modal-content">  
                <div class="modal-header">  
                     <button type="button" class="close" data-dismiss="modal">&times;</button>  
                     <h4 class="modal-title">Update Product MySQL</h4>  
                </div>  
                <div class="modal-body">  
                     <form method="post" id="insert_form">  
                          <label>Product</label>  
                          <input type="text" name="title" id="title" class="form-control" />  
                          <br />  
                          <label>Price</label>  
                          <input type="text" name="price" id="price" class="form-control" /> 
                          <br />  
                          <label>List Price</label>  
                          <input type="text" name="list_price" id="list_price" class="form-control" /> 
                          <br /> 
                          <label>Description</label>  
                          <textarea name="description" id="description" class="form-control" /></textarea>  
                          <br />  
                          <label>Condition</label>  
                          <input type="text" name="condition" id="condition" class="form-control" />  
                          <br />  
                          <input type="hidden" name="products_id" id="products_id" />  
                          <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />  
                     </form>  
                </div>  
                <div class="modal-footer">  
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
                </div>  
           </div>  
      </div>  
 </div>  
 <script>  
 $(document).ready(function(){  
      $('#add').click(function(){  
           $('#insert').val("Insert");  
           $('#insert_form')[0].reset();  
      });  
      $(document).on('click', '.edit_data', function(){  
           var products_id = $(this).attr("id");  
           $.ajax({  
                url:"fetch.php",  
                method:"POST",  
                data:{products_id:products_id},  
                dataType:"json",  
                success:function(data){  
                     $('#title').val(data.name);  
                     $('#price').val(data.address);  
                     $('#list_price').val(data.gender);  
                     $('#description').val(data.designation);  
                     $('#condition').val(data.age);  
                     $('#products_id').val(data.id);  
                     $('#insert').val("Update");  
                     $('#add_data_Modal').modal('show');  
                }  
           });  
      });  
      $('#insert_form').on("submit", function(event){  
           event.preventDefault();  
           if($('#title').val() == "")  
           {  
                alert("Title is required");  
           }  
           else if($('#price').val() == '')  
           {  
                alert("Price is required");  
           }  
           else if($('#description').val() == '')  
           {  
                alert("Description is required");  
           }  
           else if($('#condition').val() == '')  
           {  
                alert("Condition is required");  
           }  
           else  
           {  
                $.ajax({  
                     url:"insert.php",  
                     method:"POST",  
                     data:$('#insert_form').serialize(),  
                     beforeSend:function(){  
                          $('#insert').val("Inserting");  
                     },  
                     success:function(data){  
                          $('#insert_form')[0].reset();  
                          $('#add_data_Modal').modal('hide');  
                          $('#products_table').html(data);   
                     }  
                });  
           }  
      });  
      $(document).on('click', '.view_data', function(){  
           var products_id = $(this).attr("id");  
           if(products_id != '')  
           {  
                $.ajax({  
                     url:"select.php",  
                     method:"POST",  
                     data:{products_id:products_id},  
                     success:function(data){  
                          $('#products_detail').html(data);  
                          $('#dataModal').modal('show');  
                     }  
                });  
           }            
      });  
 });  
 </script>

INSERT.PHP CODE

 <?php  
 include_once 'core/init.php';
 if(!empty($_POST))  
 {  
      $output = '';  
      $message = '';  
      $title = mysqli_real_escape_string($conn, $_POST["title"]);  
      $price = mysqli_real_escape_string($conn, $_POST["price"]);  
      $list_price = mysqli_real_escape_string($conn, $_POST["list_price"]);  
      $description = mysqli_real_escape_string($conn, $_POST["description"]);  
      $condition = mysqli_real_escape_string($conn, $_POST["condition"]);  
      if($_POST["products_id"] != '')  
      {  
           $query = "  
           UPDATE products   
           SET title='$title',   
           price='$price',   
           list_price='$list_price',   
           description = '$description',   
           condition = '$condition'   
           WHERE id='".$_POST["products_id"]."'";  
           $message = 'Data Updated';  
      }  
      else  
      {  
           $query = "  
           INSERT INTO products(title, price, list_price, description, condition)  
           VALUES('$title', '$price', '$list_price', '$description', '$condition');  
           ";  
           $message = 'Data Inserted';  
      }  
      if(mysqli_query($conn, $query))  
      {  
           $output .= '<label class="text-success">' . $message . '</label>';  
           $select_query = "SELECT * FROM products ORDER BY id DESC";  
           $result = mysqli_query($conn, $select_query);  
           $output .= '  
                <table class="table table-bordered">  
                     <tr>  
                          <th width="70%">Product Name</th>  
                          <th width="15%">Edit</th>  
                          <th width="15%">View</th>  
                     </tr>  
           ';  
           while($row = mysqli_fetch_array($result))  
           {  
                $output .= '  
                     <tr>  
                          <td>' . $row["title"] . '</td>  
                          <td><input type="button" name="edit" value="Edit" id="'.$row["id"] .'" class="btn btn-info btn-xs edit_data" /></td>  
                          <td><input type="button" name="view" value="view" id="' . $row["id"] . '" class="btn btn-info btn-xs view_data" /></td>  
                     </tr>  
                ';  
           }  
           $output .= '</table>';  
      }  
      echo $output;  
 }  
 ?>

fetch.php

  <?php  
 //fetch.php  
 include_once 'core/init.php';
 if(isset($_POST["products_id"]))  
 {  
      $query = "SELECT * FROM products WHERE id = '".$_POST["products_id"]."'";  
      $result = mysqli_query($conn, $query);  
      $row = mysqli_fetch_array($result);  
      echo json_encode($row);  
 }  
 ?>

0 个答案:

没有答案