【问题标题】:does not accept text values in database query, but allows numbers only在数据库查询中不接受文本值,但只允许数字
【发布时间】:2014-03-28 08:16:54
【问题描述】:

我有一个搜索表单,可以通过一些自定义值从数据库中获取一些信息。 例如,我的表单包含搜索关键字、类别 ID 和价格

  • search keyword:文字
  • category id : int
  • price : int

现在,当进行查询以从数据库中获取某些数据时,不允许包含 search keyword 等字符的值,但只允许 category idprice 等数字。

模板页面 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 获取数据吗?你能在你的 get_serach_classifieds 函数中检查var_dump($q_s)吗?
  • 是的,一切都很好,我已经测试了一切。从数据库获取数据时的问题。

标签: php database codeigniter


【解决方案1】:

$val != 0 似乎是问题所在。与字符串比较时,这是不正确的。

这样的事情应该可以工作:

foreach ($q_s as $key => $val) {
        if ((is_string($val) && $val != "") || (is_int($val) && $val !=0)) {
            $this->db->where($key, $val);
        } 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2015-11-30
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多