gpt4 book ai didi

php - 具有一个或多个(多个)参数的搜索表单

转载 作者:行者123 更新时间:2023-11-29 20:20:15 24 4
gpt4 key购买 nike

我已经掌握了基础知识,我创建了两个文件:用户输入搜索参数的搜索表单,以及生成输入项目的结果文件。为了简单起见,我们将搜索表单文件指定为 search.php,将结果页面指定为 results.php。

搜索.php

<?php

if (!empty($_POST['id']) && isset($_POST['id'])) {
header("Location: ?m=search.results&id=".$_POST['id']."");
} elseif (!empty($_POST['major']) && isset($_POST['major'])) {
header("Location: ?m=search.results&major=".$_POST['major']."");
} elseif (!empty($_POST['college']) && isset($_POST['major'])) {
header("Location: ?m=search.results&college=".$_POST['college']."");
} elseif (!empty($_POST['name']) && isset($_POST['name'])) {
header("Location: ?m=search.results&name=".$_POST['name']."");
} elseif (!empty($_POST['id']) && !empty($_POST['college']) && !empty($_POST['major'])
&& isset($_POST['submit']) && !empty($_POST['name'])) {
echo "<div class='alert alert-danger'>No students found. Please try different parameters.</div>";
}

?>


<h4>Search</h4>

<form method="POST">
<table width="100%">

<tr><td>ID:</td><td> <input type="text" name="id" class="form-control"></textarea></td></tr>

<tr><td>Name:</td><td> <input type="text" name="name" class="form-control"></textarea></td></tr>

<tr><td>Major:</td><td><select name="major" class="form-control"><option></option><?php echo majorSelect(); ?></select></td></tr>

<tr><td>College:</td><td><select name="college" class="form-control"><option></option><?php echo collegeSelect(); ?></select></td></tr>

<tr><td colspan="2"><input type="submit" name="submit" value="Search" class="btn btn-lrg btn-primary" style="margin-top:10px;"></td></tr>

</table>
</form>

结果.php

<!-- Begin Search Parameters -->

<?php

if (isset($_GET['id'])) {
$students = $db->query("SELECT * FROM `user_details` a, `user` b WHERE a.uid = b.id AND a.uid = '".$_GET['id']."'");

while ($student = $students->fetch()) {
echo '
<tr>
<td>'.$student['uid'].'</td>
<td>'.$student['name'].'</td>
<td>'.$student['major'].'</td>
<td>'.$student['college'].'</td>
<td><a href="?m=profile&id='.$student['id'].'" style="display:block">View</a></td>

</tr>';
}
} elseif (isset($_GET['major'])) {
$students = $db->query("SELECT * FROM `user_details` a, `user` b WHERE a.uid = b.id AND a.major = '".$_GET['major']."'");

while ($student = $students->fetch()) {
echo '
<tr>
<td>'.$student['uid'].'</td>
<td>'.$student['name'].'</td>
<td>'.$student['major'].'</td>
<td>'.$student['college'].'</td>

<td><a href="?m=profile&id='.$student['id'].'" style="display:block">View</a></td>

</tr>';
}
} elseif (isset($_GET['college'])) {
$students = $db->query("SELECT * FROM `user_details` a, `user` b WHERE a.uid = b.id AND a.college = '".$_GET['college']."'");

while ($student = $students->fetch()) {
echo '
<tr>
<td>'.$student['uid'].'</td>
<td>'.$student['name'].'</td>
<td>'.$student['major'].'</td>
<td>'.$student['college'].'</td>
<td><a href="?m=profile&id='.$student['id'].'" style="display:block">View</a></td>

</tr>';
}
} elseif (isset($_GET['name'])) {
$name = $_GET['name'];

$students = $db->query("SELECT * FROM `user_details` a, `user` b WHERE a.uid = b.id AND b.name LIKE '%". $name . "%'");

while ($student = $students->fetch()) {
echo '
<tr>
<td>'.$student['uid'].'</td>
<td>'.$student['name'].'</td>
<td>'.$student['major'].'</td>
<td>'.$student['college'].'</td>
<td><a href="?m=profile&id='.$student['id'].'" style="display:block">View</a></td>

</tr>';
}
}

因此,本质上我想重写上述内容,而用户可以输入一个或多个参数,并返回所需的结果(例如姓名和大学 - &name=x&college=y 或所有项目(如果需要)。

最佳答案

当使用 PDO(而不是 mysqli)作为数据库 API 时,这是最容易做到的。

动态构建WHERE子句。我推荐的方法是将每个条件插入一个数组,然后使用 implode() 连接所有条件,用 ANDOR 连接它们> 根据您的喜好。

$wheres = array();
$params = array();
if (!empty($_GET['id'])) {
$wheres[] = 'a.uid = :uid';
$params[':uid'] = $_GET['id'];
}
if (!empty($_GET['major'])) {
$wheres[] = 'a.major = :major';
$params[':major'] = $_GET['major'];
}
if (!empty($_GET['name'])) {
$wheres[] = 'b.name LIKE :name';
$params[':name'] = '%'.$_GET['name'].'%';
}
// And so on for all parameters

$sql = "SELECT *
FROM user_details AS a
JOIN user AS b ON a.uid = b.id";
if (!empty($wheres)) {
$sql .= " WHERE " . implode(' AND ', $wheres);
}
$stmt = $db->prepare($sql);
$stmt->execute($params);

然后按照原始代码显示结果。

while ($student = $stmt->fetch()) {
...
}

关于php - 具有一个或多个(多个)参数的搜索表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540022/

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