I have a SQL database which is implemented onto a HTML webpage. I want the user to be able to type in a value into a searchbox and search a column of the table specified in a drop down menu.
For example: there are 3 columns on the table - 'first name', 'second name' and 'surname'. The user can choose one of these column names in a drop down menu and then type into a searchbox what they want to search for in the column specified. Let's say they choose the column 'surname' and want to search for 'Johnson' in that column.
The results should show all surnames with 'Johnson' in them and show the rest of the row for each found. My database is a bit different and larger than this but it applies the same. My current code can be found below:
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<meta charset="utf-8" />
<title>The 24 Hour Race</title>
<div id="title">
<a href="index.php"><img src="images/title.png"></img></a>
</div>
<p id="subtitle">For the toughest of the tough</p>
</head>
<body>
<div id="nav">
<form method="post" action="index.php?go" id="searchform">
<br>
<h3>Search in:</h3><select name="field">
<option value="rider_no.">Rider No.</option>
<option value="category">Category</option>
<option value="name">Name</option>
<option value="team_name">Team Name</option>
<option value="nationality">Nationality</option>
<option value="year_participated">Year Participated</option>
<option value="total_laps">Total Laps</option>
<option value="fastest_lap">Fastest Lap</option>
</select>
<input type="text" name="key">
<input type="submit" name="submit" value="Search"><br>
</form>
<br>
<h3>Sort by years:</h3>
<ul>
<li><a href="2007.php">2007</a></li>
<li><a href="2008.php">2008</a></li>
<li><a href="2009.php">2009</a></li>
<li><a href="2010.php">2010</a></li>
<li><a href="2011.php">2011</a></li>
<li><a href="2012.php">2012</a></li>
<li><a href="2013.php">2013</a></li>
<li><a href="2014.php">2014</a></li>
<li><a href="2015.php">2015</a></li>
</ul>
</div>
<div id="form">
<table id="t01" align="right" cellspacing="2">
<thead>
<tr>
<td bgcolor="#ccc">Rider No.</td>
<td bgcolor="#ccc">Category</td>
<td bgcolor="#ccc">Name</td>
<td bgcolor="#ccc">Team Name</td>
<td bgcolor="#ccc">Nationality</td>
<td bgcolor="#ccc">Year Participated</td>
<td bgcolor="#ccc">Total Laps</td>
<td bgcolor="#ccc">Fastest Lap</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "Highland5");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("the24hourrace");
if(isset($_POST[field]) and isset($_POST[key])) {
$results = mysql_query("SELECT ".[field]." FROM riders WHERE ".[field]." = ".[key], $connect);
} else {
$results = mysql_query("SELECT * FROM riders", $connect);
}
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['rider_no']?></td>
<td><?php echo $row['category']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['team_name']?></td>
<td><?php echo $row['nationality']?></td>
<td><?php echo $row['year_participated']?></td>
<td><?php echo $row['total_laps']?></td>
<td><?php echo $row['fastest_lap']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
The important bits which I am asking about include div id="nav" and the form below it:
<form method="post" action="index.php?go" id="searchform">
<br>
<h3>Search in:</h3><select name="field">
<option value="rider_no.">Rider No.</option>
<option value="category">Category</option>
<option value="name">Name</option>
<option value="team_name">Team Name</option>
<option value="nationality">Nationality</option>
<option value="year_participated">Year Participated</option>
<option value="total_laps">Total Laps</option>
<option value="fastest_lap">Fastest Lap</option>
</select>
<input type="text" name="key">
<input type="submit" name="submit" value="Search"><br>
</form>
And the PHP section:
<?php
$connect = mysql_connect("localhost","root", "Highland5");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("the24hourrace");
if(isset($_POST[field]) and isset($_POST[key])) {
$results = mysql_query("SELECT ".[field]." FROM riders WHERE ".[field]." = ".[key], $connect);
} else {
$results = mysql_query("SELECT * FROM riders", $connect);
}
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['rider_no']?></td>
<td><?php echo $row['category']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['team_name']?></td>
<td><?php echo $row['nationality']?></td>
<td><?php echo $row['year_participated']?></td>
<td><?php echo $row['total_laps']?></td>
<td><?php echo $row['fastest_lap']?></td>
</tr>
<?php
}
?>
If you have any questions about my code or what I am asking then please do not hesitate to ask! I found this a bit tricky to explain :)