Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Populating dropdown from SQL database

$
0
0
I am trying to create a page which populates the filter from SQL database. In current script below it is done manually. Considering I am new to php, will really appreciate if some one can advise what need to be changed in this code to achieve the same.
 
 
************************************************
<tr>
<td>
<form name="input" action="" method="post">
City: <Select name="city">
<option "Input" value="<?php echo $_POST['city']; ?>"><?php echo $_POST['city']; ?></option>
<option value="">All</option>
<option value="Jaipur">Jaipur</option>
<option value="Bangalore">Bangalore</option>
</select>
</td>
<td>
Country: <Select name="country">
<option "Input" value="<?php echo $_POST['country']; ?>"><?php echo $_POST['country']; ?></option>
<option value="">All</option>
<option value="India">India</option>
<option value="China">China</option>
</select>
</td>
<td>
Occurence: <Select name="occurence">
<option "Input" value="<?php echo $_POST['occurence']; ?>"><?php echo $_POST['occurence']; ?></option>
<option value="">All</option>
<option value="Monthly">Monthly</option>
<option value="Yearly">Yearly</option>
</select>
 
<input type="submit" value="Search Events" />
</form>
</td></tr>
 
<?php
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$whereclause = '';
$where = array();
 
if(strlen($_POST['city'])>0){
    $city = mysql_real_escape_string($_POST['city']);
    $where[] = "city = '$city'";
}
if(strlen($_POST['country'])>0){
    $country = mysql_real_escape_string($_POST['country']);
    $where[] = "country = '$country'";
}
if(strlen($_POST['occurence'])>0){
    $occurence = mysql_real_escape_string($_POST['occurence']);
    $where[] = "occurence = '$occurence'";
}
 
if (count($where) > 0) {
    $whereclause = "WHERE " . join(' AND ', $where);
}
 
$result = "SELECT * FROM Events $whereclause";
$result=mysql_query($result);
 
while($noticia = mysql_fetch_array($result)) 
  echo "<b>$noticia[Name]</b><br><img src='$noticia[Pic]' width='145' height='46' border='0'><br>  ";
   }
 
?>

 


Viewing all articles
Browse latest Browse all 13200

Trending Articles