php 具有两个SQL要求的交换机案例

idfiyjo8  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(134)

我有一个表单选择,我可以在工作日和真/假之间进行选择。我做了一个switch case语句,其中some正在工作。我的查询,我可以回显周三,如果足球是0正确的。但是如果工作日是星期三,足球是1,我仍然得到0的结果。
我是否错误地使用了switch语句?
工作日可以是星期三或星期六
足球可以是0或1(0 =假,1 =真)

超文本标记语言

<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";
62o28rlo

62o28rlo1#

您需要为这两个变量使用单独的switch/case语句。

$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";

相关问题