来自接收用户输入参数的条件调用模型方法的 CodeIgniter 查询中出现未知列错误

问题描述 投票:0回答:2

查看文件(view_searchlog.php):

<div class="form-group">
    <form class="form-inline" action="<?php echo base_url(); ?>index.php/search/accsearch" method="post">
        <select class="form-control" name="field">
            <option selected="selected" disabled="disabled" value="">Filter By</option>
            <option value="subjName">Subject</option>
            <option value="homeaddress">Location</option>
            <option value="lname">Last Name</option>
        </select>
        <input class="form-control" type="text" name="search" value="" placeholder="Find..">
        <input class="btn btn-default" type="submit" name="filter" value="Search">
    </form>
        </div>
    </div>
        </div>
<div class="col-xs-12 col-sm-6">
    <h2 class="sub-header">Search Results</h2>
    </div>
    
    <div class="row">
  <div class="col-sm-12 col-md-8"><hr/>
      <?php echo $this->session->flashdata('nores'); ?>
               <?php foreach ($users as $user): ?>
    <div class="thumbnail">
      <img src="..." alt="...">
      <div class="caption">
        <h3><?=$user->name;?></h3>
        <p><?=$user->homeaddress;?></p>
        <p><?=$user->mobileNum;?></p>
        <p><a href="#" class="btn btn-primary" role="button">Request</a> <a href="#" class="btn btn-default" role="button">View Profile</a></p>
      </div>
    </div>
              <?php endforeach; ?>
  </div>
</div>

db中,我有用户帐户主题,其中:

科目

subjID | subjName | accID
------ | -------- | -----
1      | Math     | 1

账号

 accID | subjID   | userID
------ | -------- | -----
1      | 1        | 1

用户

userID | userName | accID
------ | -------- | -----
1      | foo      | 1

我有用于搜索的 ff 代码控制器

if (isset($filter) && !empty($search)) {
    $data['users'] = $this->model_search->searchTutor($field, $search);
} elseif (($filter == 'subjName') && !empty($search)) {
    $data['users'] = $this->model_search->searchBySubj($field, $search);
} elseif (isset($filter) && empty($search)) {
    $data['users'] = $this->model_search->getlist($field);
} elseif (isset($filter) && ($search == 0)) {
    $this->session->set_flashdata('nores','<div class="alert text-center">No result matched your search.</div>');
    redirect('search/index');
}

然后我尝试搜索不在 db 中的主题名称

所以应该进入第二个 elseif

->searchBySubj();
函数

搜索型号

public function searchTutor($field, $search)
{
    $this->db->like($field, $search);
    $this->db->order_by($field);
    $query = $this->db->get('users');
    return $query->result();
}

public function searchBySubj($field, $search)
{
    $this->db->query("SELECT id as Userid, accID as AccID, subjID as subid, subjName as sname FROM users userT INNER JOIN account accT ON id = accID INNER JOIN subjects ON accID = subjID;");
    $query = $this->db->get('users', 'ASC');
    return $query->result();
}

但是出现了:

A Database Error Occurred
Error Number: 1054
Unknown column 'subjName' in 'where clause'
SELECT * FROM `users` WHERE `subjName` LIKE '%mathematics%' ESCAPE '!' ORDER BY `subjName`

当我尝试搜索位置时,第三个 elseif 会运行,它会为我提供所有列表

然后我尝试搜索不在数据库中的位置,但最后一个条件不显示闪存数据

我将

<?php echo $this->session->flashdata('nores'); ?>
放在视图中(上图)

总的来说,我只想按主题名称和我拥有的表格进行搜索。另外,如果没有记录匹配,则显示 flashdata。

这些应该如何正确完成?是什么原因导致这些错误?

我错过了什么?

php sql codeigniter activerecord
2个回答
1
投票

删除下面提到的代码,因为它仅将其指向用户表。您只能用于活动记录。

 $query = $this->db->get('users', 'ASC');

以下是正确的做法

$query = $this->db->query("YOUR QUERY");
return $query->result();

0
投票

在您的查询中,您正在查找

subjName
表中的
Subject
列:

SELECT * FROM `users` WHERE `subjName` LIKE '%mathematics%' ESCAPE '!' ORDER BY `subjName`

您需要加入主题表才能访问 subjName 列:

SELECT * FROM `users` INNER JOIN account accT ON id = accID INNER JOIN subjects ON accID = subjID WHERE `subjName` LIKE '%mathematics%' ESCAPE '!' ORDER BY `subjName`

(这是您的代码中添加条件的联接查询,它可能有效也可能无效)

© www.soinside.com 2019 - 2024. All rights reserved.