简体   繁体   English

如何使用post将从datepicker中选择的日期传递给sql查询?

[英]How to pass the date selected from datepicker to sql query using post?

This is my VIEW(roster.php) 这是我的VIEW(roster.php)

<link href="http://cdn.kendostatic.com/2012.3.1024/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2012.3.1024/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2012.3.1024/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2012.3.1024/styles/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://cdn.kendostatic.com/2012.3.1024/js/kendo.all.min.js"></script>

<form action="time_model.php" method="post">
        From: <input type="text" id="datepicker"  name="date_from"/> <br/><br/>
        To: <input type="text" id="datepicker2" name="date_to" />
        </form>
<input type="submit">
<script>
    $(document).ready(function () {
        $.get('<?=base_url()?>index.php/roster/get_emprecord', function(data) {
            if ( data.substring(0,1) == '0' ) return true;
            var emp_roster = JSON.parse(data);

            $("#grid").kendoGrid({
                dataSource: {
                    data: team_dtr,
                    pageSize: 20
                },
                groupable: true,
                sortable: {
                    mode: "multiple",
                    allowUnsort: true
                },
                pageable: {
                    buttonCount: 6
                },
                scrollable: false,
                columns: [

                    {
                        field: "user_name",
                        title: "Name",
                    },

                    {
                        field: "entry_type",
                        title: "Entry Type",
                    },

                    {
                        field: "entry_datetime",
                        title: "Date",
                    }
            ]
            });
        });
    }); 
$("#datepicker").kendoDatePicker();
$("#datepicker2").kendoDatePicker();
</script>

In my view I'm selecting 2 dates(date_from, date_to) using a datepicker. 在我看来,我正在使用日期选择器选择2个日期(date_from,date_to)。 Now I'm trying to send in my query(model) the dates that were selected in my view. 现在,我尝试在查询(模型)中发送在视图中选择的日期。 But it seems not catching it. 但似乎没有抓住它。

This is my MODEL(time_model.php) 这是我的MODEL(time_model.php)

public function get_roster($end= '($_POST[date_from])', $start= '$POST[date_to]' )
{
$sql="SELECT user_name, entry_type, entry_datetime
FROM time_entries
WHERE entry_datetime 
BETWEEN' ".$start." ' AND ' ".$end." '
ORDER BY entry_datetime";

    $query = $this->db->query($sql);
    $result = $query->result_array();

    if ($result) return $result;
    return false;
}

This is my CONTROLLER(roster.php) 这是我的控制器(roster.php)

public function get_emprecord()
 {
  $data['title'] = "Roster";
  $result = $this->time_model->get_employee_record($data)
if ( $result ) $data['dump'] = json_encode($result);
else $data['dump'] = "0 Request can not be completed right now.";

$this->load->view(roster'', $data);
  }

When I try it in my controller alone it says '0 Request can not be completed right now' . 当我仅在控制器中尝试时,它会显示“ 0请求无法立即完成” I think the post that I'm throwing in my query is not catched because its giving 0 results. 我认为我在查询中抛出的帖子没有被捕获,因为它给出了0个结果。 Definitely I'm doing something not right here :(. 绝对是我在这里做的不正确的事情:(。

All I just want is to get the record from my database ranging from the selected datepicker(date_from, date_to)taken in my view. 我只想要从数据库中获取记录,该记录的范围从我视图中选择的选定日期选择器(date_from,date_to)开始。 Then return the result from similar view. 然后从类似的视图返回结果。 I saw a lot of question seems like this but I don't understand the procedure how it was done. 我看到很多问题看起来像这样,但是我不明白该过程是如何完成的。 T_T. T_T。 Thanks in advance for those who could help and enlighten me of my mistake. 在此先感谢那些可以帮助和启发我的错误的人。

Try this: 尝试这个:

public function get_roster()
{
    $sql="SELECT user_name, entry_type, entry_datetime
    FROM time_entries
    WHERE entry_datetime 
    BETWEEN' '{$_POST['date_from']}' AND '{$_POST['date_to']}'
    ORDER BY entry_datetime";

    $query = $this->db->query($sql);
    $result = $query->result_array();

    if ($result) return $result;
    return false;
}

Try like this 这样尝试

$_POST is an associative array of variables passed to the current script via the HTTP POST method. $ _POST是通过HTTP POST方法传递给当前脚本的变量的关联数组。 If you want to access the variable you have access like $_POST['date_form']. 如果要访问变量,则可以访问$ _POST ['date_form']。

So the you can use the post directly into query. 这样您就可以直接使用帖子查询。

$sql="SELECT user_name, entry_type, entry_datetime
FROM time_entries
WHERE entry_datetime 
BETWEEN '".$_POST['date_form']."' AND ' ".$_POST['date_to']."'
ORDER BY entry_datetime";

Make sure the date format should be mysql format ie(YYY-mm-dd) 确保日期格式应为mysql格式,即(YYY-mm-dd)

Refer : http://php.net/manual/en/reserved.variables.post.php 请参阅: http : //php.net/manual/en/reserved.variables.post.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM