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

时间:2017-05-02 19:31:23

标签: php mysql

我有一个表单选择,我可以在工作日和真/假之间进行选择。我做了一个switch case语句,其中一些是有效的。根据我的询问,我可以在周三回应,如果足球是正确的0。但如果工作日是星期三,足球是1,我仍然可以从0获得结果。

我是否错误地使用了switch语句?

  

工作日可以是我们周三或周六

     

足球可以是0或1(0 =假,1 =真)

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

1 个答案:

答案 0 :(得分:1)

这两个变量需要单独的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";