我是编程的新手。我尝试设计一个管理面板,该面板可以借助datatable插件直接编辑用户数据。JSON格式存在一些错误
这是我的索引页
<html>
<head>
<title>Live Add Edit Delete Datatables Records using PHP Ajax</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<style>
body
{
margin:0;
padding:0;
background-color:#f1f1f1;
}
.box
{
width:1270px;
padding:20px;
background-color:#fff;
border:1px solid #ccc;
border-radius:5px;
margin-top:25px;
box-sizing:border-box;
}
</style>
</head>
<body>
<div class="container box">
<h1 align="center">Live Add Edit Delete Datatables Records using PHP Ajax</h1>
<br />
<div class="table-responsive">
<br />
<div align="right">
<button type="button" name="add" id="add" class="btn btn-info">Add</button>
</div>
<br />
<div id="alert_message"></div>
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th>Staff id</th>
<th>Password</th>
<th>Email_id</th>
<th>gender</th>
<th>qualification</th>
<th>course1</th>
<th>course2</th>
<th>course3</th>
<th></th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
fetch_data();
function fetch_data()
{
var dataTable = $('#user_data').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"ajax" : {
url:"fetch.php",
type:"POST"
}
});
}
function update_data(id, column_name, value)
{
$.ajax({
url:"update.php",
method:"POST",
data:{id:id, column_name:column_name, value:value},
success:function(data)
{
$('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
$('#user_data').DataTable().destroy();
fetch_data();
}
});
setInterval(function(){
$('#alert_message').html('');
}, 5000);
}
$(document).on('blur', '.update', function(){
var id = $(this).data("id");
var column_name = $(this).data("column");
var value = $(this).text();
update_data(id, column_name, value);
});
$('#add').click(function(){
var html = '<tr>';
html += '<td contenteditable id="data1"></td>';
html += '<td contenteditable id="data2"></td>';
html += '<td contenteditable id="data3"></td>';
html += '<td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs">Insert</button></td>';
html += '</tr>';
$('#user_data tbody').prepend(html);
});
$(document).on('click', '#insert', function(){
var first_name = $('#data1').text();
var last_name = $('#data2').text();
var password = $('#data3').text();
if(first_name != '' && last_name != '' && password != '')
{
$.ajax({
url:"insert.php",
method:"POST",
data:{first_name:first_name, last_name:last_name, password:password},
success:function(data)
{
$('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
$('#user_data').DataTable().destroy();
fetch_data();
}
});
setInterval(function(){
$('#alert_message').html('');
}, 5000);
}
else
{
alert("Both Fields is required");
}
});
$(document).on('click', '.delete', function(){
var id = $(this).attr("id");
if(confirm("Are you sure you want to remove this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
success:function(data){
$('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
$('#user_data').DataTable().destroy();
fetch_data();
}
});
setInterval(function(){
$('#alert_message').html('');
}, 5000);
}
});
});
</script>
这是我的fetch.php文件
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "flash");
$columns = array('staff_id', 'password','email_id','gender','qualification','course1','course2','course3');
$query = "SELECT * FROM staff ";
if(isset($_POST["search"]["value"]))
{
$query .= '
WHERE staff_id LIKE "%'.$_POST["search"]["value"].'%"
OR email_id LIKE "%'.$_POST["search"]["value"].'%"
';
}
if(isset($_POST["order"]))
{
$query .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'
';
}
else
{
$query .= 'ORDER BY id DESC ';
}
$query1 = '';
if($_POST["length"] != -1)
{
$query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$number_filter_row = mysqli_num_rows(mysqli_query($connect, $query));
$result = mysqli_query($connect, $query . $query1);
$data = array();
while($row = mysqli_fetch_array($result))
{
$sub_array = array();
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="staff_id">' . $row["staff_id"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="password">' . $row["password"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="email_id">' . $row["email_id"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="gender">' . $row["gender"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="qualification">' . $row["qualification"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="course1">' . $row["course1"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="course2">' . $row["course2"] . '</div>';
$sub_array[] = '<div contenteditable class="update" data-id="'.$row["SINO"].'" data-column="course3">' . $row["course3"] . '</div>';
$sub_array[] = '<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["SINO"].'">Delete</button>';
$data[] = $sub_array;
}
function get_all_data($connect)
{
$query = "SELECT * FROM staff";
$result = mysqli_query($connect, $query);
return mysqli_num_rows($result);
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => get_all_data($connect),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
?>
这将显示一条警告消息,例如
DataTables警告:表id = user_data-无效的JSON响应。对于 有关此错误的更多信息,请参见 http://datatables.net/tn/1
fetch.php文件显示如下错误声明
注意:未定义索引:C:\ xampp \ htdocs \ FLASH \ admin中的长度 第28行的Modify \ fetch.php
注意:未定义的索引:从C:\ xampp \ htdocs \ FLASH \ admin开始 第30行的Modify \ fetch.php
注意:未定义索引:C:\ xampp \ htdocs \ FLASH \ admin中的长度 第30行的Modify \ fetch.php
警告:mysqli_num_rows()期望参数1为mysqli_result, 在C:\ xampp \ htdocs \ FLASH \ admin Modify \ fetch.php中给出的布尔值 33
警告:mysqli_fetch_array()期望参数1为mysqli_result, 在C:\ xampp \ htdocs \ FLASH \ admin Modify \ fetch.php中给出的布尔值 39
注意:未定义索引:绘制在C:\ xampp \ htdocs \ FLASH \ admin 第62行的Modify \ fetch.php {“ draw”:0,“ recordsTotal”:4,“ recordsFiltered”:null,“ data”:[]}
老兄,请帮我解决这个问题。