我创建了一个列出现有硬件交易的页面(下面是“其他”条款),并根据与特定行相关联的条件过滤交易(' If'子句)。 Else子句显示正常,但是当我输入Criteria(例如Mouse)并选择我知道在数据库中匹配的过滤器(例如设备)时,没有显示结果,应该有。
令人沮丧的是,当我将第12行更改为:
时WHERE device = '$criteria' //This will work
由于某种原因,即使变量$ filter等于字符串" device",原始代码也无法工作。
请有人帮忙,这对我来说似乎是个错误:(
PHP代码:
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页面:
<?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 } ?>