我有以下代码,可以自动从数据库中回显学生信息。但是,当用户搜索特定学生时,我需要将相关学生的相同表格重新填充到搜索请求中。
非常感谢所有帮助!
if(!isset ($_POST['search'])){
$pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils");
$count = mysql_num_rows($pupils);
if ($count == 0 ) {
$totalpupil = "There are currently no Pupils in the system.";
} else{
while($row = mysql_fetch_array($pupils)){
?>
<tr>
<td><?php echo '<a href="profile.php? id='.$row['pupil_id'].'">'.$row['pupil_id'].'</a>'; ?></td>
<td><?php echo $row['pupil_name']?></td>
<td><?php echo $row['class_id']?></td>
<td><a href="pupiledit.php?id='.$id.'">Edit</a> | <a href="pupildelete.php?id='.$id.'">Delete</a></td>
</tr>
<?php
}
}
}
答案 0 :(得分:0)
这样的东西?
if(!isset ($_POST['search'])){
$pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils");
}
elseif(isset($_POST['search'])){
$search_param = "%".$_POST['search']."%";
$pupils = mysql_query("SELECT * FROM pupil WHERE LOWER(pupil_name) LIKE LOWER($search_param)") or die("Cant find Pupils");
}
$count = mysql_num_rows($pupils);
if($count == 0){
$totalpupil = "There are currently no Pupils in the system.";
}
else{
while($row = mysql_fetch_array($pupils)){
?>
<tr>
<td><?php echo '<a href="profile.php? id='.$row['pupil_id'].'">'.$row['pupil_id'].'</a>'; ?></td>
<td><?php echo $row['pupil_name']?></td>
<td><?php echo $row['class_id']?></td>
<td><a href="pupiledit.php?id='.$id.'">Edit</a> | <a href="pupildelete.php?id='.$id.'">Delete</a></td>
</tr>
<?php
}
我假设您正在pupil_name
栏中搜索。