qebas
qebas

Reputation: 89

Using Jquery UI datepicker to filter MySQL query

I am trying to filter a query by date range using the Jquery UI datepicker, but I am always getting a SQL error. I also would like to have a submit button for the date range, if someone can also help me with that.

The code is;

    <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))
    ?>

Upvotes: 0

Views: 1582

Answers (1)

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

You need to put quotes around $to and $from

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

Upvotes: 2

Related Questions