【问题标题】:Datatables codeigniter when using filter not work使用过滤器时数据表代码点火器不起作用
【发布时间】:2019-06-27 01:01:37
【问题描述】:

我在 codeigniter 中向我的模型写入查询,当被 ajax 访问时它返回错误 HTTP 500,但是当使用浏览器时它没有 HTTP 错误代码:

 $query = 'select album.id,
                  album.name,
                  artist.name as artist,
                  artist.name as artistid,
                  album.photo,
                  album.active,
                  album.reasonactive from ' . $this->table . 
                  ' join artist on artist.id = album.ref_artist ';
      if(isset($_POST["search"]["value"])){
         $query .= '(artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
         $query .= 'OR album.name LIKE "%'.$_POST["search"]["value"].'%" ';
      }
      if(isset($_POST["order"])){
         $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
        }
        else
        {
         $query .= ' ORDER BY album.name asc ';
        }
        if($_POST['length'] != -1)
           $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
        $result = $this->db->query($query);
        return $result->result();

这是我的 ajax 代码:

$(document).ready(function() {
  table = $('#table').DataTable({

      "processing": true, //Feature control the processing indicator.
      "serverSide": true, //Feature control DataTables' server-side processing mode.
      "order": [], //Initial no order.

      // Load data for the table's content from an Ajax source
      "ajax": {
          "url": "<?php echo base_url('Manage/get_all')?>",
          "type": "POST"
      },

      //Set column definition initialisation properties.
      "columnDefs": [
          {
              "targets": [ 0 ], //first column
              "orderable": false, //set not orderable
          },
          {
              "targets": [ -1 ], //last column
              "orderable": false, //set not orderable
          },

      ],

  });

但是当我更改并删除过滤器查询时:

   $query = 'select album.id,
                        album.name,
                        artist.name as artist,
                        artist.name as artistid,
                        album.photo,
                        album.active,
                        album.reasonactive from ' . $this->table . ' join artist on artist.id = album.ref_artist ';

代码运行成功。

在我的 codeigniter 消息中:

注意:未定义的索引:长度

注意:未定义索引:开始

注意:未定义索引:draw

当我显示 $_POST 时:

{"draw":null,"recordsTotal":2,"recordsFiltered":2,"data":[]}

我该如何解决这个问题?非常感谢您在此问题上的时间和帮助。

【问题讨论】:

  • 日志文件有什么错误?
  • Failed to load resource: the server responded with a status of 500 (Internal Server Error)。只有这个节目在控制台先生
  • codeigniter 日志中有什么内容?
  • Undefined index: draw, start, lenght 先生。
  • 这是 2019 年的老兄 - 你真的不需要让自己暴露在如此简单的 sql 注入威胁中......

标签: javascript php jquery codeigniter datatables


【解决方案1】:

您的查询有问题,因此出现 500 错误:

在你的 if 子句中你缺少一个右括号“)”,它应该说

if(isset($_POST["search"]["value"])){
   $query .= ' (artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
   $query .= ' OR album.name LIKE "%'.$_POST["search"]["value"].'%" )';
}

有关如何调试的帮助:

您可以随时打印您的最后一个查询并检查例如php管理员:

echo ($this->db->last_query());die();

在任何环境中打开错误报告,并在您的根 index.php 中替换以下内容:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

用这行代码

define('ENVIRONMENT', 'development');

这使您能够在浏览器的开发控制台中获得更确凿的错误消息

【讨论】:

  • 谢谢先生,这是正确的答案,我上次的查询有一些错误,现在已修复,但我仍然面临一些未定义数据的问题。
猜你喜欢
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 2014-08-09
  • 1970-01-01
  • 2020-05-22
相关资源
最近更新 更多