So I have a table on a server that saves posts that users enter. On this final page where you can view them, you can choose to either view everyones result, or , pick just a certain weather to view. The picking certain weather part is working, but if you choose the ‘all’ option, it wont show anything. Any idea’s why?
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>
You should really watch out for sql injections, and if possible move over to mysqli_prepare, or PDO prepared
query’s.
But your main problem is that your doing a query for
allwhich unless you have weather calledallit wont find it.The solution is to check for the
alloption and change the query depending on that.Also if
$_GET['weather']is not set you need a default, I suspect you also have error reporting off and its not throwing a Notice:Undefined an error.UPDATE (Full code, with fixes):