Add a WHERE with a select box

I have a php page which displays all the records in a table

$sql = "SELECT * FROM racks";
$result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
		echo '<div class="col-2">';
		echo '<a href="show_rack.php?id='.$row[rack_id].'" class="btn btn-link " role="button">';
		echo $row[title];
		echo '</a>';
		echo '</div>';
		echo "\r\n";
    } else {
      echo "0 results";


I want to put a select box which would add a WHERE clause to the query to only shown records by a location (there are five different locations in the racks table). Do I set the value of $sql to the value selected of something?

SELECT * FROM racks WHERE rack_location = ‘USA’ ?

No way so simple. Author should use “prepare” functionality or at least escape functions as mysqli_escape_string.

1 Like

So instead of saying this, why not provide sample code?

perhaps in the php forum, eh


1 Like

Is the select optional or required?

Do you want to display the entire list, then have the results filtered when the user chooses an option from the location choice, or do you want to have them choose the location first?

I was thinking it would go like this, at first All the results would be shown, then we’ll have a select box so the results could be filtered by location via a select box

I have an example of this in my PDO Bumpstart Application.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.