Date problem - help desperately needed

Discussion in 'PHP' started by freebie, Dec 19, 2007.

  1. #1
    Hi,

    I have a problem which relates to search a MySQL database, using PHP, searching for records between two dates.

    The search script is as follows:

    <form name="sample" action="search.php">
    <input name="start" type="text" value="" size="15">
    <a href="javascript://" onClick="getCalendarFor(document.sample.start)"><img src="img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a> </input>
    From
    <input name="end" type="text" value="" size="15">
    <a href="javascript://" onClick="getCalendarFor(document.sample.end)"><img src="img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a> </input>
    To
    <input type="submit" name="Submit" value="Submit">


    This works fine and javascript pop up window displays the date when the visitor chooses a date.

    The search script is as follows:

    $result = mysql_query("select * from table WHERE date BETWEEN '".$_POST['start']."' and '".$_POST['end']."' ") or die(mysql_error());



    The problem I have is the search script doesn't find any records between the two dates - even though I know they exist.

    The dates are stored in the MySQL database in a date type field.

    Is there something I am missing to search between two dates, or is the pop up calender causing the issue (looking at the form output it does output the dates to mm-dd-yyyy as stored in the MySQL database).

    Thanks,
    CC
     
    freebie, Dec 19, 2007 IP
  2. tekteker

    tekteker Peon

    Messages:
    476
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    output from form may change.
    please try to write (echo) the form output to a blank page to see what is going from form.
    if you see different mm-dd-yyyy format, you must edit the date type
     
    tekteker, Dec 19, 2007 IP
  3. freebie

    freebie Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply but I can see the form postings from the address bar on the search script page and the format is mm-dd-yyyy (exactly as stored in the MySQL database)

    :confused:
     
    freebie, Dec 19, 2007 IP
  4. mrburns

    mrburns Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you see the variables in the address bar then you are using a GET request but your script is using POST. You didnt specify which
    Change it so they match

    <form name="sample" method="POST" action="search.php">

    or change the script to GET and see if that helps.

    $result = mysql_query("select * from table WHERE date BETWEEN '".$_GET['start']."' and '".$_GET['end']."' ") or die(mysql_error());
     
    mrburns, Dec 19, 2007 IP
  5. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #5
    IMHO, in the mysql where clause, the date should be in the format 'yyyy-mm-dd'

    I quote from the mysql help file:

    You can create $startdate as:

    $startdate = substr($_GET['start'],0,4)) . "-" . substr($_GET['start'],5,2)) . "-" . substr($_GET['start'],8,2))

    and use this in where clause.
     
    Kuldeep1952, Dec 20, 2007 IP