Using a form to search a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cowbie
    New Member
    • Nov 2007
    • 5

    Using a form to search a database

    Hello again everyone. I sorted out my problem with tables by re-jigging the code around a little bit. However, I still have one question for now. I want to be able to search my database using a php page. So, I want to search for something between two dates (fromDate) and (toDate). So far, I have the user enter in the details into a form on one page, when they click submit I want it to show their search results.

    The response page as it stands, shows what data they entered. The query I have used it here:
    [code=php]
    $fromDate = $_POST['fromDate'];
    $toDate = $_POST['toDate'];
    $query = 'select c.Firstname, c.Lastname, v.Title from Customer as c inner join Loan as l on (c.customerId = l.customerId) inner join Video as v on (v.videoId = l.videoId) where l.date >=$fromDate and l.date <=$toDate';
    $result = mysql_query($qu ery) or die('No loans Found: ' . mysql_error());
    [/code]

    The problem I'm haiving is with ...where l.date >=$fromDate and l.date <=$toDate'; I seem to have a problem with the where clause. I thought that using $fromDate would work, but it would appear not. Can anyone share some advice with me on this one?

    Great thanks!
    Last edited by Atli; Nov 21 '07, 10:38 PM. Reason: Added [code] tags.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    [PHP]$query = 'select c.Firstname, c.Lastname, v.Title from Customer as c inner join Loan as l on (c.customerId = l.customerId) inner join Video as v on (v.videoId = l.videoId) where l.date >=$fromDate and l.date <=$toDate'[/PHP]The dates need to be enclose in quotes [PHP]$query = "select c.Firstname, c.Lastname, v.Title from Customer as c inner join Loan as l on (c.customerId = l.customerId) inner join Video as v on (v.videoId = l.videoId) where l.date >='$fromDate' and l.date <='$toDate' "[/PHP]
    Also MySql expects them in the format YYYY-MM-DD

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi Cowbie.

      Please use &#91;code] tags when posting your code examples. (See How to ask a question)

      &#91;code=ph p]PHP code goes here &#91;/code]

      Thank you.

      Comment

      Working...