gpt4 book ai didi

php - 切换具有两个 SQL 要求的大小写

转载 作者:行者123 更新时间:2023-11-29 10:50:56 25 4
gpt4 key购买 nike

我有一个表单选择,我可以在工作日和真/假之间进行选择。我做了一个 switch case 声明,其中一些正在工作。通过我的查询,我可以回显星期三以及足球是否正确为 0。但如果工作日是星期三并且足球是 1,我仍然得到 0 的结果。

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

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

最佳答案

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

关于php - 切换具有两个 SQL 要求的大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43745827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com