我有5個月的php和mysql工作經驗。我的問題是如何在複選框中選擇我的mysql數據庫表中的某些列,並在php的html表格中顯示那些選定列的數據? 請參閱下面的代碼並幫我解決這個問題。選擇mysql列與複選框,並在html表中顯示數據,php
複選框代碼:
<form role="form" id="viewallteachers" method="POST" autocomplete="on" action="">
<h4>Kindly check the items to view in the table. You have a maximum of 10 items to view at a time in the table.</h4>
<hr>
<div class="row">
<div class="col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox" name="check_list[]" value="tpicture">Picture
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="check_list[]" value="teacherID">Teacher ID
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="check_list[]" value="staffID">
Staff ID
</label>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" name="btnshowcolumns" class="btn btn-primary">Save changes</button>
</form>
現在下面是我的表顯示每個選擇的列選擇的列和數據的代碼。
<?php if(!empty($_POST['check_list'])) {
$check_list = $_POST['check_list'];
$counter = count($check_list);
for($x=0; $x<=$counter; $x++){
if(!empty($check_list[$x])){
$sql = "SELECT `".$check_list[$x]."` FROM teachers";
$db = new PDO('mysql:host=localhost:3306;dbname=gnoc_db', 'root', 'xxxx');
$select = $db->prepare($sql);
$select->execute();
while($row = $select->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
foreach ($row as $key=>$value) {
echo '<td>'.$key . ' = ' . $value .'</td>';
}
echo '</tr>';
} /* END OF IF */
} /* END OF FOR LOOP */
}
}
?>
</tbody>
</table>
以下是在圖像: image of checkbox selecting the database columns image of table show data
使用你的方法,錯誤說「數組到字符串轉換」。如何在表格中顯示陣列數據 –