Reputation: 93
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>
Upvotes: 0
Views: 62
Reputation: 46610
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 all
which unless you have weather called all
it wont find it.
The solution is to check for the all
option 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.
<?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>
Upvotes: 0
Reputation: 268364
You need to remove the WHERE
clause entirely if you select "all."
$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 */
}
Upvotes: 1