Reputation: 125
I have a form select, where I can choose between a weekday and and true/false. I made a switch case statement, where some is working. With my query I can echo out wednesday and if football is 0 correctly. But if the weekday is wednesday and football is 1 I still get the result from 0.
Am I using the switch statement incorrectly?
weekday can be we wednesday or saturday
football can be 0 or 1 (0 = false, 1 = true)
HTML
<select name="weekday" class="form-control selectpicker" >
<option value=" ">Select Weekday</option>
<option value="wednesday">Wednesday</option>
<option value="saturday">Saturday</option>
</select>
<select name="football" class="form-control selectpicker" >
<option value=" " >Practice</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
PHP
$sql = "SELECT id, weekday, football FROM footballTable";
if(isset($_POST['weekday'], $_POST['football'])) {
switch($_POST['weekday'], $_POST['football']){
case 'wednesday':
$sql .= " WHERE weekday LIKE 'Wednesday' AND football = '0' OR football = '1'";
break;
case 'saturday':
$sql .= " WHERE weekday LIKE 'Saturday'";
break;
}
}
$sql .= " ORDER BY RAND ( ) LIMIT 3";
Upvotes: 0
Views: 90
Reputation: 782130
You need separate switch/case
statements for the two variables.
$sql = "SELECT id, weekday, football FROM footballTable WHERE 1=1";
switch ($_POST['weekday'] ?? '') {
case 'wednesday':
$sql .= " AND weekday = 'Wednesday'";
break;
case 'saturday':
$sql .= " AND weekday = 'Saturday'";
break;
}
switch ($_POST['football'] ?? '') {
case '0':
case '1':
$sql .= " AND football = '{$_POST['football']}'";
break;
}
$sql .= " ORDER BY RAND() LIMIT 3";
Upvotes: 1