Ive created a page that lists existing hardware transactions ('else' clause below) and filters the transactions based on criteria associated with a specific row ('If' clause). The Else clause displays fine, but when I enter Criteria (e.g. Mouse)and select a Filter (e.g. device) that I know match in the database, no results are shown, when there should be.
The frustrating part is that when I change line 12 to:
WHERE device = '$criteria' //This will work
For some reason the original code wont work even though the variable $filter equals the string "device".
Please someone help, this seems like a bug to me :(
PHP Code:
function display_transactions() {
//Display Transactions table
GLOBAL $transactions;
GLOBAL $db;
try {
if (isset($_POST['submit_criteria'])) {
GLOBAL $transactions_sql;
$filter = $_POST['filter-by'];
$criteria = $_POST['text_criteria'];
$transactions_sql = "SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
WHERE '$filter' = '$criteria'
";
} else {
$transactions_sql = 'SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
ORDER BY datetime DESC';
echo "UNFILTERED!";
}
$transactions = $db->query($transactions_sql);
} catch (Exception $e) {
echo $e->getMessage();
}
}
HTML page:
<?php display_transactions() ?>
<?php while($row = $transactions->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['device']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['transaction_type']; ?></td>
<td><?php echo $row['ticket_id']; ?></td>
<td><?php echo $row['region']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['datetime']; ?></td>
</tr>
<?php } ?>