Forum Moderators: coopster
The database consists of students name, marks, grade, and other fields.
Now I have to use a mysql database and php script and provide a search interface where if someone enters a grade in search box, he would get all possible results of the students who have got that grade.
I hope I am clear in explaining this. If there were some php script available which revolves around the same theory, then I could do the modifications as per my needs.
Any place I could find this particular script.
TIA
First, you'll have to decide which fields are searchable and how to determine what field the user wants to search.
The easy way would be to use radio buttons or a select option box. Then you will already know which field to do the search on when the input gets to your script.
If that is not an option and you just want a search box with a button, you will have to parse the input in your script to determine what field the user meant to search.
What are all the fields that would be searchable?
I still think it wouldn't be that bad if you were to use the select dropdown to determine the field to search.
HTML:
<form action="search.php" method="post">
Search Type: <select name="fieldname">
<option value="grade">Grade</option>
<option value="name">Name</option>
</select>
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
SEARCH.PHP:
<?php
/* connect to the db */
$dbh=mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('I cannot connect to the database.');
mysql_select_db ("NAME OF DB HERE");
/* now build query */
$sql = "SELECT * FROM tablename WHERE $_POST['fieldname'] = '$query'";
/* run the query */
$result = mysql_query($sql);
mysql_close();
/* loop and display the results */
while ($row = mysql_fetch_array($result))
{
print "$row['name'] $row['marks'] $row['grade'] etc...";
}
?>
Of course this a raw example and you would most likely want to do some error checking on the user input.