php mysql form date query help

Discussion in 'MySQL' started by iangon, Sep 27, 2010.

  1. #1
    hello all-

    I am trying to do a mysql database lookup by passing a begin date and end date from an html form. I have a date column in my database which is written to using the php function date("Y-m-d"); The date column is currently formatted as a mysql DATE field. The goal is to allow the user to lookup customer rows based between a begin and end date.

    Here is my string, which fails:

    $query='select * from '.$table.' where Date between `'.$Begin_Date.'` and `'.$End_Date.'` ';

    echo mysql_error();

    the error message returns:

    Unknown column '2010-09-03' in 'where clause'


    Any help would be much appreciated - I need to get this project done!

    Much Thanks!
     
    iangon, Sep 27, 2010 IP
  2. iangon

    iangon Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hmnn.. well it was worth a shot. I went ahead and renamed the column to Cust_Date but it returns the same result. Is it possible my query is not structured properly? Also my other question is did I setup the date column properly so it can be used to query from a form? The form uses a javascript calendar to select the dates. These are then passed to the output page using action=post and are stored in php variables. I then format the variables so the dates match with this php function:

    $Begin_Date_String=htmlentities($_POST['Beginning_Date']);
    $End_Date_String=htmlentities($_POST["End_Date"]);
    $Begin_Date=date("Y-m-d", strtotime($Begin_Date_String));
    $End_Date=date("Y-m-d", strtotime($End_Date_String));

    They look like this when formatted:

    Begin Date String= 09/18/2010
    End Date String = 09/30/2010

    Begin Date = 2010-09-18
    End Date= 2010-09-30

    Thanks!
     
    iangon, Sep 27, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    The backticks make the query think the dates are columns. Use a single quote and not a backtick. Do you backticks around the column named Date. Date is a reserved work in MySQL, so you should use backticks if you name a column it.

    Try this instead.

    $query="select * from ".$table." where `Date` between '".$Begin_Date."' and '".$End_Date."' ";
    PHP:
     
    jestep, Sep 28, 2010 IP
  4. iangon

    iangon Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I believe that worked - thank you very much ;)
     
    iangon, Sep 28, 2010 IP