Codeigniter-搜索两个日期之间的记录1064错误

时间:2018-06-23 03:18:59

标签: php codeigniter-3

每次我进入chargeticket_list页面时,我都会不断收到此错误。

控制器

public function chargeticket_list() {
$data['charge_tickets'] = $this->Chargeticket_model->list_charges($this->input->post('date1'), $this->input->post('date2'));
$this->load->view( 'inc/header' );
$this->load->view( 'chargeticket/chargeticket_list', $data );
$this->load->view( 'inc/footer' );
}

模型

public function list_charges($date1, $date2) //This function returns an array
    {   $this->db->join( 'customers', 'charges.idcustomers = customers.idcustomers' );
        $this->db->where('ch_date >=', $date1);
        $this->db->where('ch_date <=', $date2);
        $query = $this->db->get('charges'); //create query
        return $query->result_array(); //creates array from query
    }

查看

<script type="text/javascript">

$(document).ready(function (){
    $('.date1').datepicker({
dateFormat: 'yy-mm-dd'});
});

$(document).ready(function (){
    $('.date2').datepicker({
dateFormat: 'yy-mm-dd'});
});
</script>

<div class="container">
<?php 
    $attributes = array('id'=>'chargeticket_list', 'class'=> 'form-horizontal');
    echo form_open('chargeticket/chargeticket_list', $attributes);
?>

<div class="form-group">
    <?php $ldata = array('class' => 'control-label col-sm-4');
    echo form_label('From','date1', $ldata ); 
    $data = array('class' => 'form-control date1','name' => 'date1');?>
    <div class="col-sm-4"><?php echo form_input($data);?></div>
</div>

<div class="form-group text-center">
    <?php $ldata = array('class' => 'control-label col-sm-4');
    echo form_label('To','date2', $ldata ); 
    $data = array('class' => 'form-control date2','name' => 'date2');?>
    <div class="col-sm-4"><?php echo form_input($data);?></div>
</div>

<div class="form-group">
    <div class="text-center">
    <?php $data = array('class' => 'btn btn-primary','name' => 'submit','value' => 'submit');
    echo form_submit($data); ?>
    </div>
</div>
<?php echo form_close();?>  
<br>

<h2 align="center"> Charge Tickets </h2>
<br>


<?php if(isset($_POST['submit'])):?>
    <table class="table table-hover table-bordered table-condensed">
    <tr class="table-header">
          <td><b>Date Created</b></td>
          <td><b>Posted</b></td>
          <td><b>First Name</b></td>
          <td><b>Last Name</b></td>
          <td><b>Charge Ticket #</b></td>
          <td><b>Grand Total</b></td>
          <td><b>View</b></td>
          <td><b>Delete</b></td>
    </tr>
    <?php foreach ($charge_tickets as $object){ ?>
    <tr>
        <td><?php echo $object['ch_date'];?></td>
        <td><?php echo $object['posted'];?></td>
        <td><?php echo ucwords(strtolower($object['fname']));?></td>
        <td><?php echo ucwords(strtolower($object['lname']));?></td>
        <td><?php echo ucwords(strtolower($object['ch_ticket_id']));?></td>
        <td></td>
        <td></td>
        <td></td>
     </tr>      
    <?php } ?>  
  </table>
<?php endif;?>
</div>

我继续遇到的错误是: 错误编号:1064

您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第4行的'NULLch_date <IS NULL'附近使用

选择*来自charges,在customers上加入chargesidcustomers = customersidcustomersch_date> {{ 1}} ISNULL <ch_date IS

文件名:models / Chargeticket_model.php

行号:76

我想我的问题是为什么在使用表单中的日期文本字段之前为什么要尝试访问数据库。

我要从另一个页面复制可以。我看不出我在做什么,与此有何不同:

控制器

NULL

模型

public function customer_search() {
    $data[ 'title' ] = 'Customer Search';
    $data[ 'results' ] = $this->Customers_model->search_customers($this->input->post('search')); //excecutes get_customers function
    $this->load->view('inc/header');
    $this->load->view('customers/customer_search', $data);
    $this->load->view('inc/footer');
}

查看

public function search_customers($search) //This function returns an array
{
    $this->db->select('idcustomers, add_name, fname, lname, city, company, marina_reg');
    $this->db->like('fname', $search);
    $this->db->or_like('lname', $search);
    $this->db->or_like('company', $search);
    $this->db->or_like('add_name', $search);
    $query = $this->db->get('customers'); //create query
    return $query->result_array(); //creates array from query
}

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

SynapseIndia-感谢您开始我的思考。我读了这篇文章:

https://www.formget.com/codeigniter-form-input/

它给了我所需的代码。这是更改:

    public
    function chargeticket_list() {
        $this->load->view( 'inc/header' );
        $this->load->view( 'chargeticket/chargeticket_list');
        $this->load->view( 'inc/footer' );
    }

    public function dates_submitted() {
        $data['charge_tickets'] = $this->Chargeticket_model->list_charges($this->input->post('date1'), $this->input->post('date2'));
        $this->load->view( 'inc/header' );
        $this->load->view('chargeticket/chargeticket_list', $data);
        $this->load->view( 'inc/footer' );
}

答案 1 :(得分:0)

我想像下面那样做

public function dates_submitted() {
$data['charge_tickets'] = array();
if($this->input->post('date1')!='' && $this->input->post('date2') !=''){
$data['charge_tickets'] = $this->Chargeticket_model->list_charges($this->input->post('date1'), $this->input->post('date2'));
}
$this->load->view( 'inc/header' );
$this->load->view('chargeticket/chargeticket_list', $data);
$this->load->view( 'inc/footer' );
}