I've looked all over for a few days now, but haven't found a solution to my problem. I'm writing some PHP to query from a MySQL database that I have setup on my WAMP server. I'm also learning PHP and HTML javascript as I go, so the syntax of both languages is still a little unfamiliar to me.
My goal is to have a drop down selector box is written in Java that allows the user to chose a filter to apply to the select query, something like this:
SELECT * from exampletable WHERE header = "selected_option"
Where 'exampletable' is the table existing in the SQL database, 'header' is a column within that table, and 'selected option' is the user's choice from the drop-down.
I have tried writing various HTML forms with actions that call the PHP file that contains the SQL query using the $_POST superglobal, but nothing seems to work. Any suggestions and examples of solutions would be amazing.
Thanks!
index.php (index.php is the front end with the user interface)
<!DOCTYPE HTML>
<html>
<form action="search.php" method="post">
<select name="family">
<option value="" selected="selected">Any family</option>
<option value="capacitory">capacitor</option>
<option value="resistor">resistor</option>
<option value="ferrite bead">ferrite bead</option>
</select>
<input name="search" type="submit" value="Search>
</form>
</html>
search.php (search.php receives the selected option value and passes it into the MySQL query)
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$con = mysqli_connect('localhost','root','kelly188','mysql');
mysqli_select_db($con,"testv2");
$varfam = $_POST['family'];
$query = "SELECT * FROM testv2 WHERE (family = $varfam)";
$result = mysqli_query($query);
if($result)
{
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['family']."</td>";
}
} else {
die(mysqli_error());
}
?>
</body>
</html>

mysqli_and the deprecated/removedmysql_functions. Further, you should look into using prepared statements and bound parameters to fix the fact that your text values need to be quoted in the query.