Reputation: 245
I am trying to learn php/mysql and working with a project where I want a submit button to show the data I select from a database.
I am for the moment using this statement, and I want it as default.
$result = mysqli_query($con,"SELECT * FROM persons");
But when someone select something else from my "select" I want it to change to something like this:
$result = mysqli_query($con,"SELECT * FROM persons where firstname='$z'");
So when someone select "Charlie" and press "show" I want only data with the firstname Charlie to be shown.
You can follow the project live here: http://www.adamskymotorkylare.se/business/
Thanks in advance Jack
<form action="#" method="post">
<select name="first_name" type="text">
<option value="All">All</option>
<option value="Charlie">Charlie</option>
<option value="Bob">Bob</option>
<option value="Sibylla">Sibylla</option>
</select>
<input type="submit" name="submit" value="Show"/>
</form>
<?php
// Create connection
$z = $_POST['first_name'];
$result = mysqli_query($con,"SELECT * FROM persons");
echo "<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td> <a href='view_more.php?id=" . $id ."'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Upvotes: 0
Views: 5339
Reputation: 408
You could do it like this:
if($_POST['first_name']!="All" && isset($_POST['first_name']))
{
$firstname=htmlentities($_POST['first_name'], ENT_QUOTES);
$query="SELECT * FROM persons WHERE firstname='".$firstname."'";
}else
{
$query="SELECT * FROM persons";
}
echo '
<select name="first_name">
<option value="All">- - - All - - -</option>
';
$dbPersons=mysqli_query($con,"SELECT DISTINCT firstname FROM persons ORDER BY firstname");
while($DataPersons=mysqli_fetch_array($dbPersons))
{
$selected='';
if($DataPersons['firstname']==$firstname)
{
$selected=' selected="selected"';
}
echo '<option value="'.$DataPersons['firstname'].'"'.$selected.'>'.$DataPersons['firstname'].'</option>';
}
echo '
</select>
';
$result = mysqli_query($con,$query);
echo "
<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td> <a href='view_more.php?id=" . $id ."'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
This way you can select any name in the DB. DISTINCT takes care of duplicates.
Upvotes: 1
Reputation: 29991
You just have to check your post data:
$query = "SELECT * FROM persons";
if(isset($_POST['first_name'])) {
$query .= " WHERE firstname='".$_POST['first_name']."'";
}
$result = mysqli_query($con, $query);
Note, that you should try to use a prepared statement for the above to be secure.
Upvotes: 2