Manipulate SQL query depending on the filters applied in PHP?

I have a HTML table that gets populated with values retrieved from the connected database table 'Attendance' on page load with the session UserID of who's logged in.

html table

To adjust this table, I'm using a collection of filters created with <form> and inputs as such:

filters panel

For Workspace No. it takes all the Workspace number's in the file and lists them as options in the dropdown so they are pre-populated values. The attendance checkboxes correlate to a checkbox with a value of 1 for Present and 0 for Late

So far I have only the filters for ID search and the Workspace No. written out but I'm unsure if this is the way to go as the SQL query will still have the WHERE ... WorkID = ? even if there has been no selection made for Workspace No. rather than being omitted from the query.

$('.apply').click(function () {
      if ($('#idSearch')[0].checkValidity()){ #checks if the form is valid
      $('#idSearch').submit();
    }
    if ($('#chosenWork') != ""){
      $('#WorkIDSearch').submit(); #submits the form to POST the Workspace No.
    }
  });

...

include("config.php");

if (isset($_POST['reqID'])){ #checks if the ID search input field (reqID) has been submitted
  $userid = $_POST['reqID'];
  
} else{                                  
  $userid = $_SESSION['sess_user_id'];
  
}

if (isset($_POST['chosenWork'])){ #checks if the dropdown for Workspace Number (chosenWork) has been selected
  $chosenwork = $_POST['chosenWork'];
  
}

if (isset($_POST['chosenWork']) && isset($_POST['reqID'])){ #checks if both are selected
  $chosenwork = $_POST['chosenWork'];
  $userID = $_POST['reqID'];
  
}


$stmt = $conn->prepare("SELECT Number, ID, Date, Log_Time, WorkID, Att_Type FROM Attendance WHERE ID = ? AND WorkID = ?";
$stmt->bind_param("ii", $userID, $chosenwork);
$stmt->execute();

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

My desired output is that any of these filters can be applied independently or combined to adjust the WHERE clause of the SQL query.



from Recent Questions - Stack Overflow https://ift.tt/3rLYhju
https://ift.tt/2OVW1aJ

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation