所以我在服务器上有一个表,用于保存用户输入的帖子。在您可以查看它们的最后一页上,您可以选择查看每个人的结果,或者选择一个特定的天气来查看。选择某些天气部分正在运行,但如果您选择“全部”选项,则不会显示任何内容。任何想法都是为什么?
View Posts
<br>
<form method="get" action="view_forum.php">
<label>Select Weather to Filter </label><br />
<select name="weather">
<option value="all">all</option>
<option value="cloudy">Cloudy</option>
<option value="sunny">Sunny</option>
<option value="windy">Windy</option>
<option value="snowy">Snowy</option>
<option value="mixy">Wintery Mix</option>
<option value="rainy">Rainy</option>
</select>
<input type="submit" value="view" />
</form>
<div id="view">
<center><img src="images/forum.png" width="589" height="97"></center>
</div>
<div id="white">
<div id="blue">
<div id="grey">
<div id="container">
<?php
$dbc = mysql_connect('html','user','password','database');
mysql_select_db('database',$dbc);
$weather = sanitize( $_GET["weather"] ); // keep your input clean
if ( $weather == "all" ) {
$sql = "SELECT * FROM stories ORDER BY id DESC";
} else {
$sql = "SELECT * FROM stories WHERE weather = '$weather' ORDER BY id DESC";
}
while( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
echo "<div class=\"names\"> {$row['name']}<br /></div>";
echo "<div class=\"weathers\">{$row['weather']}<br /></div>";
echo "<div class=\"stories\">{$row['story']}<br /></div>";
echo "<img src=\"images/line.png\" width='800' height='3'>";
echo "<br />";
}
?>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
如果选择“全部”,则需要完全删除WHERE
子句。
$weather = mysql_real_escape_string( $_GET["weather"] ); // keep your input clean
if ( $weather == "all" ) {
$sql = "SELECT * FROM stories ORDER BY id DESC";
} else {
$sql = "SELECT * FROM stories WHERE weather = '$weather' ORDER BY id DESC";
}
$result = mysql_query( $sql ) or die( mysql_error() );
while ( $row = mysql_fetch_assoc( $result ) ) {
/* Echo results */
}
答案 1 :(得分:0)
你应该注意sql注入,如果可能的话,请转到mysqli_prepare或PDO prepared 查询的。
但是你的主要问题是你在对all
进行查询,除非你有all
的天气它就找不到它。
解决方法是检查all
选项并根据具体情况更改查询。
如果没有设置$_GET['weather']
你需要一个默认值,我怀疑你也有错误报告关闭而且没有抛出一个提示:未定义错误。
<?php
if(isset($_GET['weather'])){
$_GET['weather']=mysql_real_escape_string($_GET['weather']);
//All
if($_GET['weather'] == 'all'){
$sql = "SELECT * from stories ORDER BY id DESC";
}else{
//Specific
$sql = "SELECT * from stories WHERE weather='{$_GET['weather']}' ORDER BY id DESC";
}
}else{
//Default
$sql = "SELECT * from stories ORDER BY id DESC";
}
?>
<?php
$dbc=mysql_connect('html','user','password','database') or die(mysql_error());
mysql_select_db('database',$dbc) or die(mysql_error());
?>
View Posts <br>
<form method="get" action="view_forum.php">
<label>Select Weather to Filter </label><br />
<select name="weather">
<option value="all">all</option>
<option value="cloudy">Cloudy</option>
<option value="sunny">Sunny</option>
<option value="windy">Windy</option>
<option value="snowy">Snowy</option>
<option value="mixy">Wintery Mix</option>
<option value="rainy">Rainy</option>
</select>
<input type="submit" value="view" />
</form>
<div id="view">
<center><img src="images/forum.png" width="589" height="97"></center>
</div>
<div id="white">
<div id="blue">
<div id="grey">
<div id="container">
<?php
if(isset($_GET['weather'])){
$_GET['weather']=mysql_real_escape_string($_GET['weather']);
//All
if($_GET['weather'] == 'all'){
$sql = "SELECT `name`,`weather`,`story`
FROM stories
ORDER BY id DESC";
}else{
//Specific
$sql = "SELECT `name`,`weather`,`story`
FROM stories
WHERE weather='{$_GET['weather']}'
ORDER BY id DESC";
}
}else{
//Default
$sql = "SELECT `name`,`weather`,`story`
FROM stories
ORDER BY id DESC";
}
$result = mysql_query($sql);
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_assoc($result)){
echo "<div class=\"names\"> {$row['name']}<br /></div>";
echo "<div class=\"weathers\">{$row['weather']}<br /></div>";
echo "<div class=\"stories\">{$row['story']}<br /></div>";
echo "<img src=\"images/line.png\" width='800' height='3'>";
echo "<br />";
}
}else{
echo 'No results';
}
?>
</div>
</div>
</div>
</div>