gpt4 book ai didi

php - 如何使用 php 将搜索结果插入到表中?

转载 作者:行者123 更新时间:2023-11-29 17:50:22 25 4
gpt4 key购买 nike

这是我必须搜索的代码,它只是彼此相邻地出现。我可以将 html 放入 php 中吗?这将如何完成?或者我应该在表格下方制作一个表格?

   <?php

include ('database_conn.php');

$output = '';

if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);

$query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
$count = mysqli_num_rows($query);

if($count == 0){
$output = "There was no search results!";

}else{

while ($row = mysqli_fetch_array($query)) {


$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];

$output .='<div> '.$stud_id.''.$module.''.$attendance_status.'</div>';




}

}
}

?>

这是我在 HTML 中搜索的表单

<form action ="CM0671_attendance.php" method = "post">

<input name="search" type="text" size="30" placeholder="Student ID"/>

<input class="btn btn-primary" type="submit" value="Search"/>

</form>

最佳答案

Replace your while with this:


echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Module</th>";
echo "<th>Status</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
echo "<tr>";
echo "<td>{$stud_id}</td>";
echo "<td>{$module}</td>";
echo "<td>{$attendance_status}</td>";
echo "</tr>";
}
echo "</table><br>";

关于php - 如何使用 php 将搜索结果插入到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49417907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com