使用Jquery UI datepicker过滤MySQL查询

时间:2013-06-17 20:49:45

标签: php jquery mysql

我正在尝试使用Jquery UI datepicker按日期范围过滤查询,但我总是收到SQL错误。我还希望有一个日期范围的提交按钮,如果有人也可以帮助我。

代码是;

    <script type="text/javascript" language="javascript">
            jQuery(function() {
                jQuery( "#from" ).datepicker({
                  defaultDate: "+1w",
                  changeMonth: true,
                  numberOfMonths: 1,
                  onClose: function( selectedDate ) {
                    $( "#to" ).datepicker( "option", "minDate", selectedDate );
                  }
                });
                jQuery( "#to" ).datepicker({
                  defaultDate: "+1w",
                  changeMonth: true,
                  numberOfMonths: 1,
                  onClose: function( selectedDate ) {
                    jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );
                  }
                });
            });
</script>
    <form action="" method="post">
    <label style="margin: 0 10px 0 20px;" for="from">From</label>
    <input style="padding: 3px; border-radius: 4px; opacity: none; background: #EEEEEE;" type="text" id="from" name="from" />
    <label style="margin-left: 10px;" for="to">To</label>
    <input style="padding: 3px; border-radius: 4px; background: #EEEEEE;" type="text" id="to" name="to" />
    </form>

    <?php  
    $from = $_POST['from'] ;
    $to = $_POST['to'] ;

    SELECT display_name as Author,
    FROM posts p
    WHERE p.post_status = 'publish' AND ((post_date >= $from) AND (post_date < $to))
    ?>

1 个答案:

答案 0 :(得分:2)

您需要在$to$from

周围加上引号
mysql_query("SELECT display_name as Author,
    FROM posts p
    WHERE p.post_status = 'publish' AND ((post_date >= '$from') AND (post_date < '$to'))");