Tobias Brage
Tobias Brage

Reputation: 69

SQL make empty variable select all

i have a select tag with following options attached.

<option value="">All</option>
<option value="Fiat">Fiat</option>
<option value="Volvo">Volvo</option>
<option value="Ford">Ford</option>

so if have this sql query "SELECT * FROM cars WHERE carBrand = '$carBrand'"

but if "All" is chosen i wan't to get all results printed out but instead i get nothing, i thought an empty string would just select everything but guess not, any ideas?

Upvotes: 0

Views: 76

Answers (1)

John Conde
John Conde

Reputation: 219924

Dynamically build your query:

$sql = "SELECT * FROM cars";
if (!empty($carBrand)) {
    $sql .=  " WHERE carBrand = '$carBrand'";
}

Of course, this assumes you have alredy validated and escaped $carBrand.

Upvotes: 3

Related Questions