Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Search From Help...

    Hi all... I have a search form which until I added the date was working well, e.g. the user could just enter 'James' in the FIRST NAME, and get a result. I added a search for DATE and now the user must enter a date other wise the results are 0, I want the date to be in the search but if the user just enters first name and no date I want to see the results... Hopefully this makes some sense? Here's my code...

    PHP Code:
    /*set varibles from form */
    $firstName trim($_POST['firstname']);
    $lastName trim($_POST['lastname']);
    $email trim($_POST['email']);
    $rep $_POST['rep'];
    $state $_POST['state'];
    $status $_POST['status'];
    $early_Joindate $_POST['early_Joindate']; 
    $later_Joindate $_POST['later_Joindate']; 


    $results mysql_query("SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%' 
    AND `LastName` LIKE '%$lastName%' 
    AND `Email` LIKE '%$email%' 
    AND `State` LIKE '%$state%' 
    AND `MemberApproved` LIKE '%$status%' 
    AND `rep_NBR` LIKE '%$rep%' 
    AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'
    ORDER BY `LastName`"
    ); 
    Last edited by tomyknoker; 07-15-2007 at 09:03 AM.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    You need to either default your dates if there are none or just remove the date search entirely if they dont put in a date. Currently you are searching for dates between '' and ''.

    PHP Code:
    /*set varibles from form */
    $firstName trim($_POST['firstname']);
    $lastName trim($_POST['lastname']);
    $email trim($_POST['email']);
    $rep $_POST['rep'];
    $state $_POST['state'];
    $status $_POST['status'];
    $early_Joindate $_POST['early_Joindate']; 
    $later_Joindate $_POST['later_Joindate']; 

    $sql "SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%' 
    AND `LastName` LIKE '%$lastName%' 
    AND `Email` LIKE '%$email%' 
    AND `State` LIKE '%$state%' 
    AND `MemberApproved` LIKE '%$status%' 
    AND `rep_NBR` LIKE '%$rep%' 
    "
    ;
    if(!empty(
    $early_Joindate) && !empty($later_Joindate))
    {
     
    $sql . = " AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'";
    }
    $sql .= " ORDER BY `LastName`"

    $results mysql_query($sql); 

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ahhhh perfect I love that idea... What I don't get though (let's remove the date entirely) it if they don't enter say a LastName... And just a firstname the search still works perfectly like it doesn't need a LastName, but needs a date... Hmmm did that make sense??

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ug I got the following error:

    Parse error: syntax error, unexpected T_VARIABLE on line 31

    Line 31 is
    PHP Code:
    $results mysql_query($sql); 

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Missing semi-colon at the end of the line above

    $sql .= " ORDER BY `LastName`";

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yea found it I shouldn't have been so quick to post! Thanks!

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by tomyknoker View Post
    Ahhhh perfect I love that idea... What I don't get though (let's remove the date entirely) it if they don't enter say a LastName... And just a firstname the search still works perfectly like it doesn't need a LastName, but needs a date... Hmmm did that make sense??
    You're using a LIKE search for the names but you need an exact match (within the given range) for the dates. Saying where name LIKE '%%' will match any name

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ah ok got it!

    I tried to add another date search actually I will have four in the end... I'm not getting any errors but it's giving me results as the entire database...

    Do I need to change anything on the second If?

    PHP Code:
    $sql "SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%'  
    AND `LastName` LIKE '%$lastName%'  
    AND `Email` LIKE '%$email%'  
    AND `State` LIKE '%$state%'  
    AND `MemberApproved` LIKE '%$status%'  
    AND `rep_NBR` LIKE '%$rep%'  
    "

    if(!empty(
    $early_Joindate) && !empty($later_Joindate)) 

    $sql .= "AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'"

    if(!empty(
    $early_Approveddate) && !empty($later_Approveddate)) 

    $sql .= "AND `MemberApprovedDate` >= '$early_Approveddate' && `MemberApprovedDate` <= '$later_Approveddate'"

    $sql .= "ORDER BY `LastName`"

    $results mysql_query($sql); 

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    echo out your sql so you can see what query it is running.

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It just gave me Resource id #4 Is that normal?

  • #11
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    no.
    What were you trying to echo?
    You should be echoing out the sql query not the result resource id.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •