The variable $ _GET is a query that does not work when it is passed

advertisements

Im using the following to assign a value to a variable if the variable is passed via the URL:

if (!empty($_POST)) {
    $sortBy = isset($_GET['sort']) ? $_GET['sort'] : 'mgap_ska_id';
}

here is where the variable is being passed:

$result = "SELECT mgap_ska_id, mgap_ska_id_name, SUM(DISTINCT mgap_growth) as growthtotal, SUM(DISTINCT mgap_recovery) as recovery FROM mgap_orders "
        . "WHERE account_manager_id = '" . $_SESSION['account_manager_id'] . "' GROUP BY mgap_ska_id ORDER BY '".$sortBy. "' ";

The process is working fine if the variable isnt being passed, but the variable in the query becomes unassigned when passed through the link like such:

<a href = "customer_view.php?sort=mgap_ska_id_name">

Can anyone see where my error is?

FYI Ive tried it in the query with and without the quotes.

EDIT WITH SOLUTION:

Here is how I solved the problem: O checked for both POST and GET:

 if (!empty($_GET) || !empty($_POST)) {
    $sortBy = isset($_GET['sort']) ? $_GET['sort'] : 'mgap_ska_id';
}  else {
$sortBy = 'mgap_ska_id';
}


From OP's comments

The page needs to check for a POST because it can receive such from another entry point, but the URL sending the variable is on the same page.I have to check the URL for the existence of the variable. –

Then makes use of $_REQUEST

if (!isset($_REQUEST)) {