Jump to content

Recommended Posts

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>  ";
   }
 
?>

 

Link to comment
https://forums.phpfreaks.com/topic/281320-populating-dropdown-from-sql-database/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.