0

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))
    ?>
5
  • And what is $q? Where is that defined? Commented Jun 17, 2013 at 20:55
  • Also, it doesn't appear that you are setting the format for your datepicker. If you're going to put the user input straight in (which is HIGHLY insecure), you need to format it in YYYY-MM-DD. Commented Jun 17, 2013 at 20:56
  • Sorry, there's no $q, code was adjusted. The error is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND (post_date < ))" Commented Jun 17, 2013 at 20:57
  • 1
    Unclosed <script> tag... Commented Jun 17, 2013 at 20:57
  • @gustavohenke thanks, but that's not the problem. Commented Jun 17, 2013 at 21:02

1 Answer 1

2

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'))");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.