所以,我有一個複選框,允許用戶選擇他們想要查看的列。所以,如果用戶想要例如只選擇2列(比如TicketID和Category),它將只顯示ID和票據類別的數據。這是我到目前爲止有:通過用戶在php中選擇數據庫數據填充html表
形式:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<ul>
<li><input type="checkbox" name="filter[]" value="TicketID" >Ticket ID</li>
<li><input type="checkbox" name="filter[]" value="Category" />Category</li>
<li><input type="checkbox" name="filter[]" value="Priority" />Priority</li>
<li><input type="checkbox" name="filter[]" value="Status" />Status</li>
<li><input type="checkbox" name="filter[]" value="InitialDescription" />Initial Description</li>
<li><input type="checkbox" name="filter[]" value="SubmittedDate" />Submitted Date</li>
<li><input type="checkbox" name="filter[]" value="Description" />Status Description</li>
<input type="submit" value="Refresh Filters">
</ul>
</form>
</td>
<td>
<?php
viewTicketTable($_SESSION['userID'],$_POST['filter']);
?>
viewticketTable功能:
function viewTicketTable($userID,$columns) {
/* Accepts $userID which will identify the tickets related with the user and
$columns which will filter only the columns that are required and outputs the
table with the columns passed through. */
/*
$columns_array = explode(',', $columns);
foreach($array as $array){
echo $array;
} */
foreach($columns as $filter) {
$filter = $filter . ',' ;
}
$filter = substr($filter, 0, -1);
$query = mysql_query("
SELECT $filter
FROM Ticket
LEFT JOIN TicketHistory
ON Ticket.TicketID = TicketHistory.TicketID
WHERE CustomerID = $userID;
");
/* Creation of the table */
echo '
<table border="1">
<thead>
<tr>';
foreach($columns as $tableHeader) {
$tableHeader = '<th scope="col">' . $tableHeader . '</th>' ;
echo $tableHeader;
}
echo '
</tr>
</thead>
<tbody>
';
/*Looping through the script to print out all the information.*/
while ($row = mysql_fetch_array($query) or die(mysql_error())) {
echo '
<tr>';
foreach($columns as $field) {
echo '<td>' . $row[$field] . '</td>';
}
echo '</tr>
';
}
echo ' </tbody>
</table>';
}
的問題存在於while循環。行[$ field]只填充最後一列,而不是填充它們全部。任何幫助?
在別的之前;你需要做一些事情來加強對SQL注入的查詢。現在任何人都有足夠的印記來使用瀏覽器來檢查和編輯你的頁面,可能會將某個過濾器輸入值更改爲惡意。 – Dogoferis
現在我只是想它的工作...但是,感謝您的提醒:) – Hugo