我在db
中有两个表bpi_registration
和
bpi_schoolInfo
bpi_registration中的字段是
id,id_school, first_name,last_name,city,state,email
和bpi_schoolInfo中的字段是
id_school,school_name,school_state
我正在努力创建一个搜索功能,每当我从下拉列表中选择学校名称时,学生的名字就会显示来自该特定学校的信息。当我选择学校名称时,网址如下所示:https://www.example.com/retrieve1.php?Grade=&School=kipp+spark+academy&Team=&Students=
我写的代码是:
if (isset($_GET['School']))
{
$sql= "SELECT * FROM bpi_schoolInfo
INNER JOIN bpi_registration ON bpi_registration.id_school = bpi_schoolInfo.id_school";
$userQuery = "{$sql} WHERE bpi_schoolInfo.id_school = :school_id";
$user = $db->prepare($userQuery);
$user->execute(['school_id' => $_GET['School']]);
$selectedUser=$user->fetch(PDO::FETCH_ASSOC);
if(isset($selectedUser))
{
echo '<tr>';
echo '<td>' . $selectedUser['first_name'] . '</td>';
echo '<td>' . $selectedUser['last_name'] . '</td>';
echo '<td>' . $selectedUser['email'] . '</td>';
echo '<td>' . $selectedUser['address_city'] . '</td>';
echo '<td>' . $selectedUser['address_state'] . '</td>';
echo '<td>' . $selectedUser['address_country'] . '</td>';
echo '</tr>';
}
}
但是出于某种原因,当我点击学校名称时,我无法看到该特定学校的学生名单。我假设我在查询时遇到了问题,如果有人可以帮助我,我将不胜感激。
答案 0 :(得分:0)
$_GET['School']
包含学校名称,但您将其与查询中的学校ID进行比较。它应该是:
$userQuery = "{$sql} WHERE bpi_schoolInfo.school_name = :school_id";
或者您应该更改URL以发送学校ID而不是名称。