简体   繁体   English

切换具有两个SQL要求的案例

[英]Switch case with two SQL requirements

I have a form select, where I can choose between a weekday and and true/false. 我有一个表单选择,可以在工作日和true / false之间进行选择。 I made a switch case statement, where some is working. 我做了一个switch case语句,其中有些在起作用。 With my query I can echo out wednesday and if football is 0 correctly. 通过我的查询,我可以回显星期三,如果足球正确为0。 But if the weekday is wednesday and football is 1 I still get the result from 0. 但是,如果工作日是星期三,而足球是1,我仍然会从0得到结果。

Am I using the switch statement incorrectly? 我使用switch语句不正确吗?

weekday can be we wednesday or saturday 工作日可以是星期三或星期六

football can be 0 or 1 (0 = false, 1 = true) 足球可以为0或1(0 =否,1 =正确)

HTML 的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 的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";

You need separate switch/case statements for the two variables. 您需要为两个变量使用单独的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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM