PHP/MSSQL - Filtering from User Input (HTML)

后端 未结 3 748
离开以前
离开以前 2021-01-27 10:10

I have been given this assignment, to include some sort of filtering to my current SQL query via User Input. Basically, i am looking for a filtering option, whether its some kin

3条回答
  •  孤独总比滥情好
    2021-01-27 10:59

     You can do that simply by introducing if else statement
     $where  = "";
     //receive filter option  example $_GET['week']  
      //Do some sanitizing for $_GET['week']
      if ($_GET['week']) {
      $where  =  "WHERE Test_Database.Week = $_GET['week']"
     } else if (somecondition) {
      $where  = "some query";
      }
    

    //You can add multiple condition by concatenating $where, but make sure where not repeats

    $query = "SELECT TOP 10 Test_Database.Distributor, Test_Database.Value
    FROM Test_Database
    $where  
    GROUP BY Distributor
    ORDER BY Value desc "
    

提交回复
热议问题