【问题标题】:Random Broken Search Feature in Codeigniter, Gives Database Error?Codeigniter 中的随机损坏搜索功能导致数据库错误?
【发布时间】:2020-06-22 18:43:52
【问题描述】:

这是我知道您可能需要的服务器版本信息:

  • CodeIgniter 版本:3.1.9
  • PHP 版本:7.3.6
  • MySQL 服务器版本:5.7.27

一般来说,我是 php 和网页设计的新手。

对于 CodeIgniter 的 PHP 速记工具来说尤其如此。我的一位朋友由于她的“日常工作”而变得越来越难以联系,他是几年前为本网站编写代码的人。最近,我已经独立使用它一段时间了,我学到了很多东西。但是,我最近遇到了一个小问题,我想知道是否有人可以帮助我:

不久前,我的食谱搜索功能完美无缺。到现在。我很少编辑任何构建或驱动我网站搜索功能的东西。但是,最近我在搜索字段中输入任何内容时出现以下错误:

错误截图:

https://gyazo.com/d949731310229541b33ead40a806c333

Recipe.php(配方控制器)

<?php
class Recipe extends MY_Controller{

 function __construct() {
  parent::__construct();
   $uri = $this->uri->uri_string();
   $this->session->set_flashdata('uri', $uri);
 }
    
 function index(){
  $recipe_linkname = $this->uri->rsegment(3);
  if(!$recipe_linkname){
   $recipe_linkname = $this->recipe_model->first_linkname();
  }
  $recipe = $this->recipe_model->read_record($recipe_linkname);
  if(count($recipe) == 0){
   show_404('');
   die();
  }
  $data = array(
     'title' => "Shirley's Recipes:".$recipe['name'],
   'columns' => array('toc', 'recipe'),
    'recipe' => $recipe
  );
  $this->load->view('includes/template', $data);
 }
    
 function flipbook(){
  $this->require_login();
  $this->load->library('pagination');
    
        $config['base_url'] = site_url('recipe/flipbook');
      $config['total_rows'] = $this->recipe_model->num_rows();
     $config['uri_segment'] = '3';
        $config['per_page'] = 1;
       $config['num_links'] = 1;
   $config['display_pages'] = TRUE;
   $config['full_tag_open'] = '<div id="pagination">';
  $config['full_tag_close'] = '</div>';
  
  $this->pagination->initialize($config);
  $recipe = $this->recipe_model->read_records($config['per_page'], $this->uri->segment(3))->row_array();
    $data = array(
     'title' => "Shirley's Recipes:".$recipe['name'],
   'columns' => array('toc', 'recipe_flipbook'),
    'recipe' => $recipe
  );
  $this->load->view('includes/template', $data);
 }
  
 function search(){
   if($this->input->method() === 'post'){
    $search = $this->input->post('query');
   $results = $this->recipe_model->search($search);
      $data = array(
     'title' => "Shirley's Recipes:Search Results",
   'columns' => array('toc', 'recipe_search'),
   'recipes' => $results,
   );
   $this->load->view('includes/template', $data);
   }
   else{
    redirect('recipe');
   }
 } 
    
 function add(){
  $this->require_login();
  if($this->input->post('submit')){
   $data = array(
          'name' => $this->input->post('name'),
    'buttontext' => $this->input->post('buttontext'),
      'linkname' => $this->input->post('linkname'),
           'img' => $this->input->post('img'),
          'time' => $this->input->post('time'),
      'category' => $this->input->post('category'),
   'subcategory' => $this->input->post('subcategory'),
  ' ingredients' => $this->input->post('ingredients'),
     'equipment' => $this->input->post('equipment'),
   'preparation' => $this->input->post('preparation'),
    'directions' => $this->input->post('directions')
   );
   $recipeid = $this->recipe_model->create_record($data);
   redirect('recipe/'.$this->input->post('linkname'));
  }
  else{
   $data = array(
         'title' => "Shirley's Recipes:Add New Recipe",
       'columns' => array('toc', 'admin/recipe_edit'),
        'recipe' => array('name' => '',
    'buttontext' => '',
      'category' => '',
   'subcategory' => '',
      'linkname' => '',
           'img' => '',
          'time' => '',
   'ingredients' => '',
     'equipment' => '',
   'preparation' => '',
    'directions' => '')
   );
   $this->load->view('includes/template', $data);
  }
 }

 function edit(){
  $this->require_login();
  $id = $this->uri->segment(3);
  if($this->input->post('submit')){
   $data = array(
          'name' => $this->input->post('name'),
    'buttontext' => $this->input->post('buttontext'),
      'linkname' => $this->input->post('linkname'),
           'img' => $this->input->post('img'),
          'time' => $this->input->post('time'),
      'category' => $this->input->post('category'),
   'subcategory' => $this->input->post('subcategory'),
   'ingredients' => $this->input->post('ingredients'),
     'equipment' => $this->input->post('equipment'),
   'preparation' => $this->input->post('preparation'),
    'directions' => $this->input->post('directions')
    );
    $this->recipe_model->update_record($id, $data);
    redirect('recipe/'.$this->input->post('linkname'));
  }
  else{
   $recipe = $this->recipe_model->read_record($id);
   $data = array(
     'title' => "Shirley's Recipes:Edit Recipe : ".$recipe['name'],
       'columns' => array('toc', 'admin/recipe_edit'),
            'id' => $id,
    'recipe' => $recipe
   );
   $this->load->view('includes/template', $data);
  }
 }
    
 function delete(){
  $this->require_login();
  $this->recipe_model->delete_record($this->uri->segment(3));
  redirect('/');
 } 
}
// End - recipe.php
?>

Recipe_model.php(配方模型)

<?php
class Recipe_model extends CI_Model {

 function create_record($data){
  if(isset($data['buttontext']) and $data['buttontext'] == ''){
   $data['buttontext'] = NULL;
  }
  $this->db->insert('recipe', $data);
  return $this->db->insert_id();
 }
  
 function read_records($limit, $offset){
  $this->db->order_by('name', 'asc');
  if(isset($limit) && isset($offset)){
   $this->db->limit($limit, $offset);
  }
  $q = $this->db->get('recipe');
  return $q;
 }
  
 function read_record($key){
  if(is_numeric($key)){
   $this->db->where('id', $key);
  }
  else{
   $this->db->where('linkname', $key);
  }
  $q = $this->db->get('recipe');
  return $q->row_array();
 }
  
 function first_linkname(){
  $this->db->select('linkname, name');
  $this->db->order_by('name', 'asc');
  $this->db->limit(1);
  $q = $this->db->get('recipe');    
  return $q->row()->linkname;
 }
        
 function read_names(){
  $this->db->select('linkname, name');
  $this->db->order_by('name', 'asc');
  $q = $this->db->get('recipe');
  return $q->result();
 }
  
 function read_names_categories(){
  $this->db->select('buttontext, linkname, name, category, subcategory, img, time');
  $this->db->order_by('name', 'asc');
  $q = $this->db->get('recipe');
  return $q->result();  
 }
    
 function search($search){
  $terms = explode(" ", $search);
  $match = "";
  foreach($terms as $term){
   $match .=  $term;
  }
  $querystr = "SELECT *, MATCH(name, linkname, time, img, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') as score FROM recipe WHERE MATCH(name, linkname, time, img, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') ORDER BY score DESC;";
  $q = $this->db->query($querystr);
  return $q->result();
 }

 function update_record($id, $data){
  if(isset($data['buttontext']) and $data['buttontext'] == ''){
   $data['buttontext'] = NULL;
  }
  $this->db->where('id', $id);
  $this->db->update('recipe', $data);
 }
  
 function delete_record($id){
  $this->db->where('id', $id);
  $this->db->delete('recipe');
 }
  
 function num_rows(){
  return $this->db->get('recipe')->num_rows();
 }
}
// End - recipe_model.php
?>

recipe_search.php(搜索输出)

<div id="recipelist" class="content">
 <h1>Recipe Search</h1>
  <div class="recipesearch">
   <ol>
    <?php if(!empty($recipes)): foreach($recipes as $recipe): ?>
     <li><a href="/recipe/<?=$recipe->id ?>"><?=$recipe->name ?></a><span class="confidence"><?=$recipe->score ?></span></li>
     <?php endforeach; ?>
   </ol>
  </div>
  <?php else: ?>
  <h3>No matching recipes</h3>
  <?php endif; ?>
</div>

最后,这里是数据库结构和字段名称的截图。

数据库截图:

https://gyazo.com/b17307885e9ea4e7c619efba707a7e44

我希望这是你们帮助我解决问题所需的一切。如果将来可以避免损坏,我也愿意以不同的方式重写 PHP。非常感谢您提供的任何和所有帮助。

再次感谢您!

【问题讨论】:

  • 请不要将代码和错误消息作为图像发布,而是以代码格式的文本发布,因为我们都不能复制、粘贴和运行图像。有关这方面的更多信息,请在提问时查看 SO 上的Why may I not upload images of code?另请查看How to Ask,了解更多现场最佳实践。
  • 如果我发布了代码图片,我深表歉意。但是,据我所知,我只发布了我收到的错误页面或数据库表配置的图像。任何时候代码是合适的或可能的,我都使用了代码块。我使用的唯一屏幕截图是输出。我很抱歉,但我看不到我在哪里使用图像代替代码块。如果您查看了屏幕截图,您会看到除了代码 sn-p 块中之外没有发布任何代码。
  • 这是一个固定评论(我应该删除“代码”部分),我指的是错误消息。由于我不知道gyazo.com,所以我不点击它,许多其他用户对点击未知链接也有同样的感觉。如果链接失效,会发生什么?那么我们没有更多的错误信息来强调您的问题了吗?顺便提一句。您可以使用 imgur 在您的问题中放置图像,这是平台支持的方法。只需单击图像图标并浏览、拖放或图像或链接(最大 2 MiB)

标签: php mysql codeigniter


【解决方案1】:

在数据库截图的底部,它列出了表上的索引。 Keyname 为 name_2 的那个不再匹配您使用 MATCH() 查询的所有字段。

例如错误截图中的time和img没有出现在name_2索引中(可能有更多的缺失字段)。

您需要运行数据库查询来为 MATCH 查询中的所有字段添加索引。像这样的东西(但添加所有字段):

ALTER TABLE `recipe` ADD FULLTEXT(name, linkname, time, img, ingredients, equipment...);

See this question for more detail

【讨论】:

  • 哦,我明白你的意思了!谢谢你。如果可以的话,快速跟进?为什么如果我在搜索字段中输入字母“r”并且我知道我有很多以字母 r 开头的结果或其中包含字母 r,我得到的结果为零?再次感谢您。
  • 没问题。搜索是否使用 JavaScript 调用?检查是否没有设置要求搜索词具有一定长度才能运行搜索查询。顺便说一句,MySQL FULLTEXT 只会索引具有 4 个或更多字符的术语。这意味着它不会匹配 3 个字符或更短的单词的记录。 See this for more detail and how to change it if needed.
  • @J.Davis 这个答案解决了问题吗?如果是这样,您可以投票或接受它作为正确答案吗?
猜你喜欢
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多