我正从我的一个小型数据库表开发一个小型搜索引擎。但无论我搜索它,它只显示一个结果。请帮我正确地做到这一点......
由于
HTML表单:
<form action="search_page.php" method="post">
<select name="search_type" id="search_type" required>
<option value="*" selected disabled>Search By...</option>
<option value="name">Name</option>
<option value="mailing_address">Mailing Address</option>
<option value="permanent_address">Permanent Address</option>
<option value="current_prof_1">Profession</option>
</select>
<input type="search" name="search" placeholder="Search Here..." required>
<button type="submit" name="search_start" class="search_button">Search</button>
</form>
和php查询代码:
<?php
if (isset($_POST['search_start'])) {
$search_by = $_POST['search_type'];
$search_for = $_POST['search'];
$search_query="SELECT * FROM member Where $search_by like '%$search_for%'";
$search_pull = mysqli_query($connection, $search_query);
$search_count = mysqli_num_rows($search_pull);
if ($search_count > 0 ) {
$serial = 1;
while ($row = mysqli_fetch_array($search_pull)) {
$member_id = $row["id"];
$name = ucwords($row["name"]);
$batch = $row["batch"];
$search_members ="<tr>";
$search_members .="<td>$serial</td>";
$search_members .="<td>$name</td>";
$search_members .="<td>$batch</td>";
$search_members .="</tr>";
++$serial;
}
}
}
?>
答案 0 :(得分:4)
更改
$search_members ="<tr>";
到
$search_members .="<tr>";
您每次都在循环中初始化打印字符串。
您需要将其与相应的记录连接起来。
有了这个错字,你的结果就是每次用while循环中的最新记录来写。
另外,请初始化
$search_members = '';
,它不应返回未定义的变量错误。
答案 1 :(得分:0)
用以下代码替换您的代码:
<?php
if (isset($_POST['search_start'])) {
$search_by = $_POST['search_type'];
$search_for = $_POST['search'];
$search_query="SELECT * FROM member Where $search_by like '%$search_for%'";
$search_pull = mysqli_query($connection, $search_query);
$search_count = mysqli_num_rows($search_pull);
if ($search_count > 0 ) {
$serial = 1;
$search_members='';
while ($row = mysqli_fetch_array($search_pull)) {
$member_id = $row["id"];
$name = ucwords($row["name"]);
$batch = $row["batch"];
$search_members .="<tr>";
$search_members .="<td>$serial</td>";
$search_members .="<td>$name</td>";
$search_members .="<td>$batch</td>";
$search_members .="</tr>";
++$serial;
}
}
}
?>