【发布时间】:2017-07-15 12:35:12
【问题描述】:
我有一个已由数据库填充的下拉列表。我正在尝试获取与其表 ID 相关的数据。我可以获取数据,但它返回的是所有数据,而不是特定于某项运动的 ID。当我从下拉列表中选择一项运动时,然后单击我要返回该数据的按钮。
我有两个文件,一个是动作,一个是下拉列表
<form method='POST' action="sportSearch.php">
<fieldset>
<div id="dropDownList">
<select value="sport" name="sport">
<?php
try {
$dropDownSport = $pdo->prepare('SELECT sportName FROM sport');
$dropDownSport->execute();
$dropDown = $dropDownSport->fetchAll();
} catch(PDOException $e) {
$error = 'Select statement error';
include 'error.html.php';
exit();
}
foreach($dropDown as $row) {
echo'<option value='.$row["sportID"].'>'.$row["sportName"].'</option>';
}
?>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</fieldset>
</form>
下面我试图找到特定运动的 id,但它只是输出
$sportId = $_POST['sport'];
$where = "";
$search_sport_id = false;
if(is_numeric($sportId)) {
$search_sport_id = true;
$where = "WHERE sport.sportID = :sportID";
}
try {
$selectSport = $pdo->prepare ('SELECT athlete.athleteID,
athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName,
athlete.gender, event.eventName, sport.sportName, athlete.gender,
athlete.image, athlete.medal
FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport
ON sport.sportID = athlete.sportID '.$where);
if($search_sport_id == true) {
$selectSport->execute(array(':sportID' => $sportId));
} else {
$selectSport->execute();
}
} catch(PDOException $e) {
$error = 'Select statement error';
include 'error.html.php';
exit();
}
然后输出到表格
<table>
<tr>
<th>athleteID</th>
<th>eventID</th>
<th>sportID</th>
<th>lastName</th>
<th>firstName</th>
<th>eventName</th>
<th>sportName</th>
<th>gender</th>
<th>image</th>
<th>medal</th>
</tr>
<?php
if(!empty($selectSport)) {
foreach($selectSport as $row) {
echo'<tr>';
echo'<td>'.$row['athleteID'].'</td>';
echo'<td>'.$row['eventID'].'</td>';
echo'<td>'.$row['sportID'].'</td>';
echo'<td>'.$row['lastName'].'</td>';
echo'<td>'.$row['firstName'].'</td>';
echo'<td>'.$row['eventName'].'</td>';
echo'<td>'.$row['sportName'].'</td>';
echo'<td>'.$row['gender'].'</td>';
echo'<td><img src="photos/'.$row['image'].'"</td>';
echo'<td>'.$row['medal'].'</td>';
echo'</tr>';
}
}
?>
</table>
【问题讨论】: