SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple beginners question

    Hi guys,
    I'm fairly new to php and mysql (I've bought about 5 books and working my way through them now).

    I know how to do sql queries, but something I'm having difficulty researching is how to search a database with user input.

    For example, lets say I have a table called users, with name, email, and gender as columns in that table.

    How can I make a form that will allow the user to search that table for a specific name, and email address, and gender.

    So when they press submit, they will only get the results that have all of those fields matching.

    What I've managed to get so far is php to return ALL of the records from the columns that I've specified for my specific project, but am stumped as to how someone can sort it.

    I'm guessing that I'm done this the wrong way around, i.e. returning all the data first and then attempting to filter/sort it afterwards.

    Perhaps this belongs in the php section of the forum, not sure.

    I image that I would have form fields, titled name, email and gender, and somehow assign each field a variable that I would somehow link to a specific column in a table?

    Can someone help to put me on the right track here please

    regards
    steve

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Part of your question is PHP/HTML: How to make a form (HTML) and how to get and elaborate the form values after the form has been sent (PHP).
    How to get the wanted data from your DB is a MySQL question

    Let's take a look at your table called users, with name, email, and gender as columns in that table.

    Let's suppose you've got your form up and running, and someone is performing a search. He fills out the form and presses the send button.
    Your PHP script receives the form values, validates them and constructs the query.
    In the end, that query will look something like
    Code:
    SELECT name, email, gender
    FROM users
    WHERE name LIKE '%formnamevalue%'
    AND email like '%formemailvalue%'
    AND gender = 'formgendervalue'
    If you want to show the results in a certain order, you'll add a ORDER BY clause as well (for example ORDER BY name)

    Displaying the results of this query is again a matter of PHP.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    thanks for the quick reply.
    I'll re-post the php related questions into the php forum

    What does % mean in an sql query?

    And the way you've written the gender part, presumably you didn't put "LIKE" because you know it will only be "male" or "female", or however you want to format it?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    % in a LIKE statement means the following:
    - LIKE 'text' : behaves the same as = 'text'
    - LIKE 'text%' : will find all values that begin with 'text' ('text', 'texting', 'text again', etc)
    - LIKE '%text' : will find all values that end with 'text' ('text', 'another text', 'notext', etc)
    - LIKE '%text%' : will find all values that contain 'text' ('text', 'texting', 'notext', 'another text for you', etc)

    I wrote the gender part like that, because knowing it can only be male or female, I presumed that you'll use a drop down box or radio boxes in your form. That way the user can only choose between the allowed values and won't be able to make mistakes.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah so it's the same as a wildcard then. that's very clear thanks.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by stevex33 View Post
    ah so it's the same as a wildcard then. that's very clear thanks.
    Yep, wildcard, that's the word I was looking for...


Bookmarks

Posting Permissions

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