tabledit(编辑和删除功能)无法正常工作

时间:2018-10-11 20:00:32

标签: php mysql session tabledit

我在使用tabledit插件时遇到了一些麻烦。当我保存更新的列时,它似乎工作正常……但是刷新页面时,它又回到了尝试更新之前的状态。我将代码缩短了一点,使其更具可读性。请帮忙!我不知道我在做什么错。

我在下面发布我的代码

非常感谢

contact_view.php-这将显示联系人以及“编辑/删除”按钮

<?php
if(!isset($_SESSION['username'])){
   header("location:login.php");
}
$connect = mysqli_connect("localhost", "root", "", "contacts");
$query = "select contact_id, first_name, last_name from contact where
  contact_owner = '".$_SESSION ["username"]."'";
$result = mysqli_query($connect, $query);
?>
<html>  
<head>  
<title>Contact List</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>            
<script src="jquery.tabledit.min.js"></script>
</head>  
<body>  
<div class="container">  
   <br />  
   <br />  
   <br />  
<div class="table-responsive">  
    <h3 align="center">Contact List</h3><br />  
    <table id="editable_table" class="table table-bordered table-striped">
     <thead>
     <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th       
      </tr>
     </thead>
     <tbody>
     <?php
     while($row = mysqli_fetch_array($result))
     {
      echo '
  <tr>
   <td type="text" name="contact_id">'.$row["contact_id"].'</td>
   <td type="text" name="first_name">'.$row["first_name"].'</td>
   <td type="text" name="last_name">'.$row["last_name"].'</td>
  </tr>
      ';
     }
     ?>
     </tbody>
    </table>
   </div>  
  </div>  
 </body>  
</html>  
<script>
$(document).ready(function(){  
     $('#editable_table').Tabledit({
      url:'action.php',
      columns:{
       identifier:[0, "contact_id"],
      editable:[[1, 'first_name'], 
                [2, 'last_name']] 
          },
          restoreButton:false,
         onSuccess:function(data, textStatus, jqXHR)
          {
           if(data.action == 'delete')
           {
            $('#'+data.id).remove();
           }
          }
         });

    });  
      </script>

action.php-执行查询

<?php  
//action.php
session_start();
include 'dbc.php';

$input = filter_input_array(INPUT_POST);

$first_name = mysqli_real_escape_string($conn, $input["first_name"]);
$last_name = mysqli_real_escape_string($conn, $input["last_name"]);

if($input["action"] === 'edit')
{
 $query = "
 UPDATE 'contact' 
 SET 'first_name' = '".$first_name."', 
     'last_name' = '".$last_name."' 
   WHERE 'contact_id' = '".$input["contact_id"]."'
                                             ";

 mysqli_query($connect, $query);

}
if($input["action"] === 'delete')
{
 $query = "
 DELETE FROM 'contact' 
 WHERE contact_id = '".$input["contact_id"]."'
 ";
 mysqli_query($connect, $query);
}

 echo json_encode($input);

?>

0 个答案:

没有答案
相关问题