php - Building an SQL query using multiple (optional) search fields -


i have form going used search through table of support tickets.

the user can search few difficult optional fields.

  • date (to/from)
  • ticket status
  • engineer
  • ticket contact

i'm wondering best way deal optional search filters. have query takes in parameters user. if user searches using both , dates query want include between. have write different query if user searches from. or query when user has not added date parameters? if status dropdown blank? query?

any clear great!

jonesy

build query in parts. start whatever constant in query, , add on more sql depending on conditions:

$query = "select ...   ...   [where conditions going present]";  if (isset($_post['date_from']) && isset($_post['date_to'])) {   $query .= ... // query code dealing dates }  if (isset($_post['status'])) {   $query .= ... // deal status }  // etc.  // once have query built, execute $result_set = mysql_query($query); 

this code skeleton, that's how construct query.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -