刚刚开始使用参数绑定,我已经阅读了所有文档和相当多的问题/答案,但我似乎无法使其发挥作用。
只需通过获取请求进行排序。
public function getAttributeGroups($data = array()) {
$params = array();
$sql = "
SELECT *
FROM {$this->db->prefix}attribute_group ag
LEFT JOIN {$this->db->prefix}attribute_group_description agd
ON (ag.attribute_group_id = agd.attribute_group_id)
WHERE agd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
$sort_data = array(
'agd.name',
'ag.sort_order'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
//$sql .= " ORDER BY " . $data['sort'];
$sql .= " ORDER BY :sort";
$params[':sort'] = $data['sort'];
} else {
$sql .= " ORDER BY agd.name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql, $params);
return $query->rows;
}
引用的行是原始的,排序很好,但在分析代码时可以进行注射。
以上操作会执行查询并返回数据,但它不会按传入的绑定进行排序。
我错过了什么?