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 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with searching a table

    I'm writing a web site which will allow the user to search for a item using more than one user input, I'm struggling to get my head round the logic though.

    for example if a user inputs four entries into a web form what is the best way to search the mysql database using these four entries? i've been thinking about using a switch statement in php, or using numerous 'ifs' in sql code, any ideas on what would be the best way, or any other ways?

    also if any of the web form fields are left empty can you make php / mysql discard these?

    any ideas on the best way to tackle this would be great, i've had a look round the web and nothing seems to answer my questions, although i'm sure there will be answers out there, so any links to info would also be great

    Cheers
    Steve

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perhaps

    SELECT .... WHERE col1 LIKE (input1) OR col2 LIKE (input2) ...... ?

    im not sure whether the szntax of LIKE is correct, check it yourself.

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Code:
    SELECT * FROM tablename WHERE fieldname = $input1 OR fieldname = $input2 OR fieldname = $input3 OR fieldname = $input4
    PHP Code:
    $sql "SELECT * FROM tablename WHERE";
    $firstrun true;
    foreach (
    $_POST as $key => $val)
    {
      if(
    $val != "")
        {
           if (
    $firstrun == true)
             {
                
    $sql .=" $key = '$val' ";
                
    $firstrun false;
             }
           else
             {
               
    $sql .=" OR $key = '$val' ";
             }
        }

    thats if your fieldnames are the same as your forminputs else just replace $key with the fieldname you're searching

    assuming your form inputs are the same name as your fieldnames, if you

  • #4
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    would it be ok to replace the 'or's' in the sql as 'ands'? i can't see that being a problem?
    thanks for the help
    steve

  • #5
    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 steviecee
    would it be ok to replace the 'or's' in the sql as 'ands'? i can't see that being a problem?
    thanks for the help
    steve
    Thats no problem

  • #6
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats great, thanks again
    steve


  •  

    Posting Permissions

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