我有几个表格用于在我建立的网站上进行报告。我已经开始使用DataTable,并试图转换为使用服务器端处理。我遵循示例here并修改它以使用我已有的内容。这是填好的文件(ServerSide.php):为什么我的桌子不显示?
<?php
$Page = '';
if (isset($_GET['PageName']))
{
//echo "<br>Page = Get<br>";
$Page = $_GET['PageName'];
}
elseif (isset($_POST['PageName']))
{
//echo "<br>Page = Post<br>";
$Page = $_POST['PageName'];
}
//For testing just this page
//if($Page == '')
//{
// $Page = 'TableHeadings';
//}
include 'DBConn.php';
$headings = array();
$hsql = "select Headings from TableHeadings where TableName = '$Page' order by Id";
$getHeadings = $conn->query($hsql);
$rHeadings = $getHeadings->fetchALL(PDO::FETCH_ASSOC);
$CountHeadings = count($rHeadings);
$tsqlHeadings = '';
$ColumnHeader = array();
for ($row = 0; $row < $CountHeadings; $row++)
{
$headings[$row] = $rHeadings[$row]["Headings"];
$tsqlHeadings = $tsqlHeadings . "[" . $headings[$row] . '],';
}
foreach($headings as $index => $columnName)
{
$ColumnHeader[] = array('db'=>$columnName,'dt'=>$index);
}
//DB table to use
$table = $Page;
//Table's primary key
$primaryKey = 'id';
//Array of database columns which should be read and sent back to DataTables
$columns = $headings;
//SQL server connection information
$sql_details = array(
'user'=> 'EngsysWebUser',
'pass'=> 'Fr0ntier2016',
'db'=> 'EngSys',
'host'=> 'MAFINFWWAPV01'
);
$connectionInfo = array("Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($servername, $connectionInfo);
if($conn === false) {
die(print_r(sqlsrv_errors(), true));
}
// Get data to display
$Query = "
SELECT count($primaryKey) over() as Row_Count, ".str_replace(" , ", " ", implode(", ", $columns))."
FROM $table";
$rResult = sqlsrv_query($conn, $Query);
if($rResult === false) {
die(print_r(sqlsrv_errors(), true));
}
// Data set length after filtering
$iFilteredTotal = sqlsrv_num_rows($rResult);
// Total data set length
$sQuery = "SELECT COUNT($primaryKey)
FROM $table";
$rResultTotal = sqlsrv_query($conn, $sQuery);
$aResultTotal = sqlsrv_fetch_array($rResultTotal, SQLSRV_FETCH_ASSOC);
$iTotal = $aResultTotal;
// Output
$output = array(
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"Data" => array()
);
while ($aRow = sqlsrv_fetch_array($rResult,SQLSRV_FETCH_ASSOC))
{
$row = array();
for ($i=0 ; $i<count($columns) ; $i++)
{
if ($columns[$i] == "version")
{
// Special output formatting for 'version' column
$row[] = ($aRow[ $columns[$i] ]=="0") ? '-' : $aRow[ $columns[$i] ];
}
else if ($columns[$i] != ' ')
{
// General output
$row[] = $aRow[ $columns[$i] ];
}
}
$output['Data'][] = $row;
}
echo json_encode($output);
?>
然后,我有我的其他文件,如果我理解正确的应该调用前一个(ServerSide.php)和地点JSON响应,作为表进行格式化,变成我的表格的tbody(RunningServerSide.php):
<?php
$Page = '';
if (isset($_GET['PageName']))
{
$Page = $_GET['PageName'];
}
elseif (isset($_POST['PageName']))
{
$Page = $_POST['PageName'];
}
include 'DBConn.php';
$headings = array();
$hsql = "select Headings from TableHeadings where TableName = '$Page' order by Id";
$getHeadings = $conn->query($hsql);
$rHeadings = $getHeadings->fetchALL(PDO::FETCH_ASSOC);
$CountHeadings = count($rHeadings);
$tsqlHeadings = '';
$ColumnHeader = array();
for ($row = 0; $row < $CountHeadings; $row++)
{
$headings[$row] = $rHeadings[$row]["Headings"];
$tsqlHeadings = $tsqlHeadings . "[" . $headings[$row] . '],';
}
$Edit = 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<?php require 'StyleLinks.php'; ?>
<?php include 'DataTableLinks.php'; ?>
</head>
<body>
<table class="NormalTable display nowrap" style="width: 100%; border: 1px" id="DataTable">
<thead>
<tr><?php echo "\n";
if($Edit == 1)
{?>
<th class="cell">Edit</th><?php echo "\n";
}
foreach($headings as $heading)
{?>
<th class="cell"><?php echo $heading; ?></th><?php echo "\n";
}?>
</tr>
</thead><?php echo "\n";?>
</table>
</body>
</html>
然后只是为了清楚起见,我已经包括在造型第二个文件的head
几个文件目的。它们包括我为表格创建的CSS以及我从DataTables下载的格式化文件。再加上DataTableLinks.php文件,我有这样的初始化数据表:
<script>
$(document).ready(function()
{
$('#DataTable').DataTable(
{
"lengthMenu": [[25, 50, 75, 100, 150], [25, 50, 75, 100, 150]],
"ScrollX": true,
"dom": '<"top"Biflp<"clear">>rt<"bottom"ip<"clear">>',
buttons: [{ extend: 'collection', text: 'Selection', buttons: ['selectAll', 'selectNone'] }, { extend: 'collection', text: 'Export', buttons: ['excel', 'csv', 'pdf']}],
fixedHeader: { header: true, footer: false },
select: true,
"processing": true,
"serverSide": true,
"ajax": { "url": "ServerSide.php", "dataType": "jsonp", "success": function(data){$('#DataTable').append(data);} }
});
});
</script>
我已经纠正我在控制台(F12)中发现的问题和工作不再有存在的任何问题。但我仍然只能得到2个按钮和表头,然后出现一个弹出窗口:
DataTables警告:表id = DataTable - 无效的JSON响应。有关此错误的更多信息,请参阅http://datatables.net/tn/1
该错误消息中的链接表示检查开发人员工具的网络部分中的响应。在那里它对我来说看起来很好,但是我从来没有使用过DataTable,而现在我只使用JSON和AJAX工作了一周。该反应是这样的:
和预览标签是这样的:
[小博](http://bobby-tables.com/)说***脚本有风险SQL注入攻击。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! *** SQL注入!*** *这不仅仅是为了早餐!* –
你没有成功的功能ajax –
@JayBlanchard这是一个内部唯一的网站,这样就会限制威胁。并且没有用于向我的SQL添加任何内容的开放文本框,只有几个下拉列表。我计划将SQL转换为使用PDO'prepare'语句,但我希望首先得到这个工作。 – Mike