php - Codeigniter 中的随机损坏搜索功能导致数据库错误?
问题描述
这是我知道您可能需要的服务器版本信息:
- 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。非常感谢您提供的任何和所有帮助。
再次感谢你!
解决方案
在数据库屏幕截图的底部,它列出了表上的索引。键名为 name_2 的那个不再匹配您使用 MATCH() 查询的所有字段。
例如,错误截图中的 time 和 img 没有出现在 name_2 索引中(可能缺少更多字段)。
您需要运行数据库查询来为 MATCH 查询中的所有字段添加索引。像这样的东西(但添加所有字段):
ALTER TABLE `recipe` ADD FULLTEXT(name, linkname, time, img, ingredients, equipment...);
推荐阅读
- python - Can't import tkcalendar or Calendar from tkcalendar,显示 ImportError,很可能是循环导入
- android - 再次打开片段后recyclerview项目的位置发生变化
- neo4j - 在 Neo4J 中返回前 % 的结果
- ios - Xcode - 找不到 -lBVLinearGradient 的库
- javascript - 如何迭代地将数组从 localStorage 渲染到 react-google-chart?
- amazon-web-services - Terraform、AWS 和 STS 请求中包含的安全令牌无效
- javascript - 试图在 javaScript 中增加 simon 游戏级别
- java - 堆栈类分隔元素
- flutter - 错误:未找到:'dart:ui' import 'dart:ui' - Flutter
- c - C语言:如何使用字符串从n中找到由数字创建的最大数