我想显示数据库中的数据。我创建了一个搜索表单,但显示结果时出错。附上错误的截图。
在数据库中,由员工姓名和分支组成。分支下拉列表,我从数据库中获取它。分支的例子是学院计算机科学和数学。
我认为错误发生在模型中。 有人可以帮我修改我的代码
CONTROLLER
function filter_directory()
{
$search_term = $this->input->post('search_term');
$search_branch = $this->input->post('search_branch');
$data['search_term'] = $search_term;
$data['search_branch'] = $search_branch;
$data['branch_name'] = $this->direktori_model->get_branch_list();
$data['search_term'] = $this->input->get('search_term');
$data['search_branch'] =$this->input->get('search_branch');
$data['results'] = $this->direktori_model->get_directory_results($data['search_term'], $data['search_branch'], $config['per_page'], $page);
$this->load->view('directory/directory_list', $data);
}
MODEL
function get_directory_results($search_term, $search_branch, $limit=10, $offset=0)
{
$sql = "SELECT * FROM Staff_Profile
WHERE Name LIKE '%$search_term%'
AND Branch = {$search_branch}
AND Enabled = 'Y'
AND Lang ='BM'
ORDER BY ID ASC
OFFSET {$offset} ROWS
FETCH NEXT {$limit} ROWS ONlY";
$query = $this->db->query($sql);
return $query->result();
}
查看
<?php echo validation_errors();?>
<?php echo form_open("direktori/filter_directory", array('method' => 'get'));?>
<div class="row collapse">
<div class="medium-4 columns ">
<?php echo form_input(array('name' => 'search_term', 'id' => 'search-box', 'value' => $search_term)); ?>
</div>
<div class="medium-4 columns ">
<?php echo form_dropdown("search_branch", $branch_name, "", 'id="branch_id"');?>
</div>
<div class="medium-4 columns ">
<?php echo form_submit('search', 'SEARCH', 'class="button expand"'); ?>
</div>
</div>
<?php echo form_close(); ?>
答案 0 :(得分:0)
在您的查询中,您必须在BRANCH
之后添加引用您的查询:
SELECT * FROM staff_profile
where name Like '%anuar%'
and branch = BAHAGIAN KEWAIGAN & PENGURUSAN <-- ERROR
and enabled = 'Y' and lang = 'BM'
order by id asc offset o rows fetch next 10 rows only
修正查询:
SELECT * FROM staff_profile
where name Like '%anuar%'
and branch = 'BAHAGIAN KEWAIGAN & PENGURUSAN'
and enabled = 'Y' and lang = 'BM'
order by id asc offset o rows fetch next 10 rows only
答案 1 :(得分:0)
错误进入附近分支。请把分支放在引号内。
$sql = "SELECT * FROM Staff_Profile
WHERE Name LIKE '%$search_term%'
AND Branch = '$search_branch'
AND Enabled = 'Y'
AND Lang ='BM'
ORDER BY ID ASC
OFFSET {$offset} ROWS
FETCH NEXT {$limit} ROWS ONlY";
答案 2 :(得分:0)
在模型中使用codeigniter语法
function get_directory_results($search_term, $search_branch, $limit=10, $offset=0){
$this->db->order_by('ID','ASC');
$this->db->like('Name',$search_term);
$this->db->where('Lang','BM');
$this->db->where('Enabled','Y');
$this->db->where('Branch',$search_term);
$query=$this->db->get('Staff_Profile',$limit,$offset);
return $query->result();
}
以这种格式,您可以移植到任何其他BD,并向您展示您遇到的麻烦。