2
我有一个搜索表单,通过一些自定义值从数据库获取一些信息。 对于exampe,我的表单包含一个搜索关键字和类别id和价格不接受数据库查询中的文本值,但只允许数字
search keyword
:文本category id
:整数price
:整数
现在,进行查询时从数据库中获取一些数据不允许包含search keyword
等字符的值,但只允许数字如category id
,price
。
模板页面HTML
<form action="<?= base_url() ?>classifieds/search/result/" method="get">
<input type="text" name="search_keyword" id="search_keyword" />
<span id="price_fromto">
<input type="text" name="price_from" id="price_from" />
<input type="text" name="price_to" id="price_to" />
</span>
<select name="currency" id="currency">
<option value="">currency</option>
<option value=""></option>
</select>
<select name="service" id="service">
<option value="">Offer type</option>
<option value="wanted"></option>
<option value="sale">for sale</option>
</select>
<input type="submit" name="sbmtSearch" id="sbmtSearch" value="search" />
</form>
控制器代码
$this - > load - > model('classifieds'); // load model
$q_s = array(
'name' = > $this -> input -> get('search_keyword', true),
'category_id' = > $this -> input -> get('search_category', true),
'type' = > $this - > input -> get('type', true),
'price >' = > $this -> input -> get('price_from', true),
'price <' = > $this -> input -> get('price_to', true),);
// configartion
$output['query'] = $this->classifieds->get_serach_classifieds($q_s); // get content in this model
}
$data['content'] = $this -> load -> view('category', $output, true); // append model content in content variable
$data['title'] = 'search result'; // model model name the page title
$this -> load -> view('index', $data); // load master page (Container)
公告模型
function get_serach_classifieds($q_s) {
$this->db->where('state', 1);
foreach ($q_s as $key => $val) {
if ($val != "" && $val != 0) {
$this->db->where($key, $val);
}
}
return $this->db->get('content');
}
创建(错误)
SELECT * FROM (`content`) WHERE `state` = 1 AND `category_id` = '4' AND `price` > '100' AND `price` < '800' LIMIT 15
应该查询是这个样子(正确)
SELECT * FROM (`content`) WHERE `state` = 1 AND 'name' = 'bmw' AND 'type' = 'wanted' AND `category_id` = '4' AND `price` > '100' AND `price` < '800' LIMIT 15
是什么使得查询不接受文字在我的代码问题的查询值?
你通过得到获取数据?你可以在你的get_serach_classifieds函数中检查'var_dump($ q_s)'。 – Rikesh
是的,一切都很好,我测试了一切。从数据库获取数据时的问题。 –