Selecting Column Names dynamically with AND in mySQL

Whenever we have multiple form fields to search in database by using AND operator for the fields only for which form fields are filled. Here is the quick fix.

Example

<?php
if ((! empty($_POST['Name'])) || (! empty($_POST['Email'])) || (! empty($_POST['CurrentDate'])) || (! empty($_POST['Amount'])) ) {
    
    $con = mysqli_connect("localhost", "root", "", "fees");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    $bigQuery = "SELECT * FROM StudentRecords where ";
    $andCount = 0; //AND check counter 
    
    if (! empty($_POST['Name'])) {
        $bigQuery .= "Name LIKE '%" . $_POST['Name'] . "%'";
        $andCount ++; //Incremented counter
    }
    
    if (! empty($_POST['CurrentDate'])) {
    // Checking if AND is to be suffixed or not  
        $bigQuery .= ($andCount == 0) ? ("CurrentDate = " . $_POST['CurrentDate'] . " ") : (" AND CurrentDate = " . $_POST['CurrentDate'] . " ");
        $andCount ++;
    }
    
    if (! empty($_POST['Email'])) {
        // Checking if AND is to be suffixed or not
        $bigQuery .= ($andCount == 0) ? ("Email LIKE '%" . $_POST['Email'] . "%' ") : (" AND Email LIKE '%" . $_POST['Email'] . "%' ");
        $andCount ++;
    }
    
    if (! empty($_POST['Amount'])) {
        $bigQuery .= ($andCount == 0) ? ("amount = " . $_POST['Amount'] ." ") : (" AND amount = " . $_POST['Amount'] . " ");
        $andCount ++;
    }
    
    //Execute Query
    $result = mysqli_query($con, $bigQuery);
    
    if ($result) {
        
        while ($row = mysqli_fetch_array($result)) {
            echo "<ul>";
            echo '<li>' . $row['Name'] . '</li>';
            echo '<li>' . $row['CurrentDate'] . '</li>';
            echo '<li>' . $row['amount'] . '</li>';
            echo "</ul>";
        }
    }
}
?>