Jump to content

Search records by dropdown menu


aianne

Recommended Posts

Hello I am having a trouble with creating page that will search records from database using dropdown menu, when I select a category, for example By: Gender and after clicking the Submit button, there is no records displays but when I select By: ALL, the records displays. What's wrong with my code? I'm sorry I'm fairly new to PHP. Here is my code:

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="" method="POST" name="records">
<label>SEARCH: </label>
<select name="select">
<option value="all" selected="selected">ALL</option>
<option value="year">By Year</option>
<option value="gender">By Gender</option>
<option value="course">By Course</option>
</select>
<input name="submit" value="GO" type="submit" />
<br />
</form>
</body>
</html>


<?php

include('collegeinfo_connect.php');

  mysql_connect("$server", "$user", "$pass")or die("cannot connect"); 
  mysql_select_db("$db")or die("cannot select DB");


if (isset($_POST['submit']))
{

       $selection = $_POST['select']; 
       
       if($selection == "all") 
         $query = "SELECT * FROM collegeinfo_tbl"; 

       else if($selection == "year") 
         $query = "SELECT * FROM collegeinfo_tbl WHERE Year='year'"; 

       else if($selection == "gender") 
         $query = "SELECT * FROM collegeinfo_tbl WHERE Gender='gender'"; 

       else if($selection == "course") 
         $query = "SELECT * FROM collegeinfo_tbl WHERE Course='course'"; 

      

}

if (isset($query))
{

  $result = mysql_query($query) or die(mysql_error()); 	   

echo "<br /> <br />";  
echo "<table border='1' cellpadding='10'>";
  echo "<tr> <th>ID Number:</th> <th>First Name:</th> <th>Last Name:</th> <th>Gender:</th> <th>Year:</th> <th>Course:</th> </tr>";	   
       
   while ($row = mysql_fetch_array($result)) { 
    

echo "<tr>";
    echo '<td>' . $row['ID'] . '</td>';
    echo '<td>' . $row['FirstName'] . '</td>';
    echo '<td>' . $row['LastName'] . '</td>';
    echo '<td>' . $row['Gender'] . '</td>';
    echo '<td>' . $row['Year'] . '</td>';
echo '<td>' . $row['Course'] . '</td>';
    echo "</tr>"; 
  } 


  echo "</table>";


}

?>

Link to comment
Share on other sites

$query = "SELECT * FROM collegeinfo_tbl WHERE Gender='gender'";

[/php[

 

Do you honestly have a row with this value 'gender'?  More than likely you have a row for 'gender' but it's value is Male or Female.  You'll need to either create chained dropdown selects that will give the option for male or female upon selecting 'by Gender', or you'll need to figure a new way to send the data you wish to receive.

Link to comment
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.