I have a bootstrap 4 form posting data to a PHP page. One of the variables is $_POST['age'];
I want to use this variable in a manner that when $_POST['age']
equals NULL
, $age = 'IN("SELECT age FROM profiles")
otherwise $minage = $age-3 and $maxage = $age+3
. I have tried the following code with different variations but it doesn't work for the situation when $_POST['age'] = NULL
$age = $_POST['age'];
$minage = $age - 2;
$maxage = $age + 3;
if($age == NULL) {
$given_age = 'IN("SELECT age FROM profiles")';
}else {
$given_age = 'BETWEEN '. $minage.' AND '. $maxage;
}
echo $given_age;
The html form is as follows:
<div class="form-group">
<label for="age">Preferred Age Group</label>
<select class="form-control" name="age" id="age" required>
<option data-hidden="true" value="Null">No Preference</option>
<option data-hidden="true" value="20">18-22 Years</option>
</select>
</div>
The outcome of the $given_age
is to be used for a MySQL query.