[英]PHP/MYSQL - Select option value not being sent?
I have the following code which should return all of the teams within a certain region. 我有以下代码,应返回特定区域内的所有团队。 I have a database of football teams that contains tables for Teams and States.
我有一个足球队数据库,其中包含“队”和“州”的表。 The teams table has a foreign key reference to a states table, and the states table has an attribute for regions (north, south, east, west).
组表具有对状态表的外键引用,状态表具有区域(北,南,东,西)的属性。
I have the following html/php code on my main page: 我的主页上有以下html / php代码:
<div>
<form method="post" action="regions_filter.php">
<fieldset>
<legend>Filter Teams By Region</legend>
<select name="Region">
<?php
if(!($stmt = $mysqli->prepare("SELECT DISTINCT region FROM states"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
$stmt->close();
?>
</select>
<input type="submit" value="Run Filter"/>
</fieldset>
</form>
</div>
Below is the regions_filter.php file code: 以下是regions_filter.php文件代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body>
<div>
<table>
<tr>
<td>Teams By Region</td>
</tr>
<tr>
<td>School Name</td>
<td>State Name</td>
<td>State Capital</td>
<td>State Population</td>
<td>Region</td>
</tr>
<?php
if(!($stmt = $mysqli->prepare("SELECT teams.school_name, states.name, states.capital, states.population, states.region FROM teams
INNER JOIN states ON states.id = teams.state_id
WHERE states.region = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("s",$_POST['Region']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
if(!$stmt->bind_result($school, $state, $capital, $population, $region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
while($stmt->fetch()){
echo "<tr>\n<td>" . $school . "\n</td>\n<td>" . $state . "\n</td>\n<td>" . $capital . "\n</td>\n</td>"
. $population . "\n</td>\n<td>" . $region . "\n</td>\n</tr>";
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
When I go to run the filter on my main page, I am taken to the regions_filter.php page with no results. 当我在主页上运行过滤器时,我被带到regions_filter.php页面,没有结果。 The only thing that shows up is the pre-coded html table at the top of the regions_filter.php page.
唯一显示的是regions_filter.php页面顶部的预编码html表。 I believe the error is somewhere in the code snippet below.
我相信错误在下面的代码片段中。 I have tried different variations with the option value but can't seem to crack it:
我已经尝试了选项值的不同变化,但似乎无法破解:
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
Any pointers in the right direction would be greatly appreciated. 朝正确方向的任何指针将不胜感激。
I believe that the states.id on your where statement is not region id. 我相信您的where语句上的States.id不是区域ID。 If you are filtering $region as a parameter from the tag to the select statement state.id = ?
如果要从标签到选择语句state.id =?过滤$ region作为参数。 (state.id = $region) it will not give any results.
(state.id = $ region)它不会给出任何结果。 because your are filtering via states.
因为您正在通过状态进行过滤。 Note: This is just my first understanding on the above code
注意:这只是我对以上代码的初步了解
I don't think you have any errors on this...it's just that you might be filtering the wrong attribute ^_^ 我认为您对此没有任何错误...只是您可能过滤了错误的属性^ _ ^
There is an error populating the regions select there is an extra white space before and after the region name. 填充区域时发生错误,选择区域名称前后都有多余的空白。 It should be like:
应该是这样的:
while($stmt->fetch()){
echo '<option value="' . $region . '">' . $region . '</option>\n';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.