我正在尝试使用不同的下拉过滤器(成绩,学校,团队,学生)实施搜索,但我的代码出现了严重问题。当我加载页面“retrieve1.php”时,我只能看到一个框,即等级,也是空的。我将不胜感激任何帮助。以下是我的代码 -
SLOC
account1.php
<?php
require 'account1.php';
echo "<body style='background-color:#DCDCDC'>";
$sql= "SELECT * FROM bpi_registration LEFT JOIN bpi_schoolInfo on
bpi_registration.id_school = bpi_schoolInfo.id_school ";
$query=$db->query($sql);
function grade() {
$result = $db->query('SELECT * FROM bpi_classInfo');
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['class_name'] . "'>" . $row['class_name'] . "</option>";
}
}
function school() {
$result = $db->query('SELECT * FROM bpi_schoolInfo');
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['school_name'] . "'>" . $row['school_name'] . "</option>";
}
}
function team() {
$result = $db->query('SELECT * FROM bpi_teamProfile');
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['team_name'] . "'>" . $row['team_name'] . "</option>";
}
}
function students() {
$result = $db->query('SELECT * FROM bpi_registration');
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['id']. "'>" . $row['first_name'].' '.$row['last_name']. "</option>";
}
}
?>
<form action="retrieve1.php" method="GET">
<select name="Grade">
<option value="" selected="selected">Choose Grade</option>
<?php grade() ?>
</select>
<select name="School">
<option value="" selected="selected">Choose School</option>
<?php school() ?>
</select>
<select name="Team">
<option value="" selected="selected">Choose Team</option>
<?php team() ?>
</select>
<select name="Students">
<option value="" selected="selected">Choose Students</option>
<?php students() ?>
</select>
<input type="submit" value="Find" />
</form>
<table width="600" border="2">
<tr>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="97"> <div align="center">State </div></th>
<th width="59"> <div align="center">Country </div></th>
<tr>
<?php
if (isset($_GET['Students']))
{
//echo $_GET['Students'];
$userQuery = "{$sql} WHERE bpi_registration.id = :user_id";
$user = $db->prepare($sql);
$user->execute(['user_id' => $_GET['Students']]);
$selectedUser=$user->fetch(PDO::FETCH_ASSOC);
}
?>
<?php
if(isset($selectedUser))
{
echo $selectedUser['email'];
echo $selectedUser['address_city'];
echo $selectedUser['address_state'];
echo $selectedUser['address_country'];
}
?>
我收到错误
<?php
$db = new PDO('mysql:host=localhost;dbname=test',$user, $pass);
?>
答案 0 :(得分:1)
您报告的错误是由于变量范围问题造成的。换句话说,函数不知道$db
是什么,因为它超出范围,$db
存在于全局范围内,但不存在于每个函数范围内。
同样,->FetchAll()
将返回一个数组,如果您使用while循环来处理查询中的结果集,则应使用->fetch()
,因为这将每次调用返回一行并可用于一段时间循环。
此外,您运行的第一个查询,然后稍后尝试修改搜索条件将无效。调用->query()
后,无法修改查询。因此,我将该代码移动到实际使用的位置,并删除了对->query()
我还在输出中添加了一些表格行HTML,以便数据适合表格结构。
下面修改后的代码应该会让您朝向目标或下一个错误。
<?php
require 'account1.php';
echo "<body style='background-color:#DCDCDC'>";
function grade($db) {
$result = $db->query('SELECT * FROM bpi_classInfo');
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['class_name'] . "'>" . $row['class_name'] . "</option>";
}
}
function school($db) {
$result = $db->query('SELECT * FROM bpi_schoolInfo');
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['school_name'] . "'>" . $row['school_name'] . "</option>";
}
}
function team($db) {
$result = $db->query('SELECT * FROM bpi_teamProfile');
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['team_name'] . "'>" . $row['team_name'] . "</option>";
}
}
function students($db) {
$result = $db->query('SELECT * FROM bpi_registration');
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo "<option value='" . $row['id']. "'>" . $row['first_name'].' '.$row['last_name']. "</option>";
}
}
?>
<form action="retrieve1.php" method="GET">
<select name="Grade">
<option value="" selected="selected">Choose Grade</option>
<?php grade($db) ?>
</select>
<select name="School">
<option value="" selected="selected">Choose School</option>
<?php school($db) ?>
</select>
<select name="Team">
<option value="" selected="selected">Choose Team</option>
<?php team($db) ?>
</select>
<select name="Students">
<option value="" selected="selected">Choose Students</option>
<?php students($db) ?>
</select>
<input type="submit" value="Find" />
</form>
<table width="600" border="2">
<tr>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="97"> <div align="center">State </div></th>
<th width="59"> <div align="center">Country </div></th>
</tr>
<?php
if (isset($_GET['Students']))
{
$sql= "SELECT *
FROM bpi_registration
LEFT JOIN bpi_schoolInfo on
bpi_registration.id_school = bpi_schoolInfo.id_school ";
//echo $_GET['Students'];
$userQuery = "{$sql} WHERE bpi_registration.id = :user_id";
$user = $db->prepare($sql);
$user->execute(['user_id' => $_GET['Students']]);
$selectedUser=$user->fetch(PDO::FETCH_ASSOC);
if(isset($selectedUser))
{
echo '<tr>';
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>
}
}
echo '</table>`;
?>