I have a PHP document that query’s a MYSQL database based on the input values from a search form. Everything is working perfectly except I am struggling to add one more feature which I believe would make the entire application much more user friendly.
My table in my database is basically storing information on a variety of social clubs. It has data regarding their location, name, booking_price, alcohol_permitted. The bit I need help with is associated with alcohol_permitted. In the database its stores 1 or 2, 2 meaning yes alcohol is permitted and 1 meaning no its not.
Currently my form looks like this:
<form>
<label for="alcohol_check">Search clubs where alcohol permitted?</label>
<input type="radio" id="alcohol_check1" value="1" /><label for="radio1">No</label>
<input type="radio" id="alcohol_check2" value="2" /><label for="radio2">Yes</label>
<input type="radio" id="alcohol_check3" value="???" /><label for="radio3">Show both</label>
<label for="club_name">Enter social club name:</label>
<input type="text" id="club_name" name="club_name"/>
</form>
My PHP looks like this:
$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()
$query="SELECT * FROM social_clubs
WHERE name = $user_social_club_name_input
AND
WHERE alcohol_permitted = $user_alcohol_permitted_selection";
The problem is at the moment, my query will only retrieve social clubs that either serve alcohol or don’t. Because the values passed at the moment from the alcohol_permitted form are either 1 or 2.
What can I do to my query so that if the user selects option ‘both’, it retrieves social clubs that serve alcohol and clubs that don’t? I cant figure it out! Do I need an if statement in my query?
Thanks for checking this out, sorry if I didn’t explain this very well.
This should work.
Additionally if column
alcohol_permittedcontains only values 1 or 2. I’d suggest you change the column type to bool like therealmitchconnors suggested in his answer.Then, you can trim down the conditional statement to:
Please note the
!.