douzhi8244 2019-07-18 08:37
浏览 49
已采纳

有条件地将复选框值添加到PHP MySQLi查询中的最佳方法是什么,以及它们相关的OR?

I am looking for some suggestions on how I can turn this messy code into something more cleaner, and efficient.

Apologies it's not the cleanest code, but I hope you can understand it. I have 4x GET parameters, (P, O, N, C).

These GET parameters are results of checkboxes, and will have either value ON, or wont be passed in the URL string.

If the checkbox is ON, I need to append to the SQL query, along with an OR after it - if the succeeding GET parameter is also ON (to avoid an unnessicary OR at the end).

I'm guessing the answer to this would be storing the GET parameters into an array, and a while loop for each one?

$select = "SELECT * FROM cif_schedules WHERE tiploc_code = '$t'";
$select .= " AND (";
if($_GET['w'] == "on"){$select .= "cif_schedules.CIF_stp_indicator = 'P' ";}
if($_GET['o'] == "on" && ($_GET['w'] == "on")){$select .=" or ";};
if($_GET['o'] == "on"){$select .= "cif_schedules.CIF_stp_indicator = 'O' ";}
if($_GET['s'] == "on" && ($_GET['o'] == "on" || $_GET['w'] == "on")){$select .=" or ";};
if($_GET['s'] == "on"){$select .= "cif_schedules.CIF_stp_indicator = 'N' ";}
if($_GET['c'] == "on" && ($_GET['s'] == "on" || $_GET['o'] == "on" || $_GET['w'] == "on")){$select .=" or ";};
if($_GET['c'] == "on"){$select .= "cif_schedules.CIF_stp_indicator = 'C' ";}
$select .= ")";
$select .= " AND deleted >= '$maxdate' AND created <= '$maxdate'";

Im sanitising the user's input so don't worry about security etc. Thanks

  • 写回答

1条回答 默认 最新

  • doujiyuan0211 2019-07-18 08:55
    关注

    I'd make a conversion array, which translate the input value to the one in the DB. Walk through this array and build the query.

    Something like this:

    // FormValue => DbValue
    $conversions = ['w' => 'P', 'o' => 'O', 's' => 'N', 'c' => 'C'];
    $ors = [];
    
    foreach($conversions as $input => $value) {
        // Check if $input is in $_GET and if its value is 'on'
        if (isset($_GET[$input]) && $_GET[$input] == 'on') {
            // Push in $ors
            $ors[] = "cif_schedules.CIF_stp_indicator = '{$value}'";
        }
    }
    

    So you'll get all the OR in the array $ors that you'll just have to implode and add it to $select if needed:

    $select = "SELECT * FROM cif_schedules WHERE tiploc_code = '$t'";
    // Check if there are some OR
    if (!empty($ors)) {
        // Add the ORs to the query
        $select.= ' AND ('.implode(' OR ', $ors).')';
    }
    $select .= " AND deleted >= '$maxdate' AND created <= '$maxdate'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?