#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    How to use form field as filter to query Mysql table?


    Hi

    I am new in Php and My Sql.
    I have created in Mysql a table
    *Table1
    organisation
    title
    country

    I made a php form.
    In the form, I have 2 fields organisation and title both are list fiels
    here is what I want to do.
    1-
    I whant organisation to collect list from organisation in table1 but only the first per organisation. exemple It migh have 10 entry with organisation called hospital1. I want the list to show hospital1 once not 10.
    2-
    For the fiel title in the form, I want the list to show only tilte in organisation alredy selected in the field1.

    All I need for those is just the Mysql sintax.
    Thank you for your assistance
  2. #2
  3. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    970
    Originally Posted by motombo
    Hi

    I am new in Php and My Sql.
    I have created in Mysql a table
    *Table1
    organisation
    title
    country

    I made a php form.
    In the form, I have 2 fields organisation and title both are list fiels
    here is what I want to do.
    1-
    I whant organisation to collect list from organisation in table1 but only the first per organisation. exemple It migh have 10 entry with organisation called hospital1. I want the list to show hospital1 once not 10.
    2-
    For the fiel title in the form, I want the list to show only tilte in organisation alredy selected in the field1.

    All I need for those is just the Mysql sintax.
    Thank you for your assistance
    You'll probably want to put an id on the table then, then do:

    PHP Code:
    select from table1 where id 
    And you'll have the row you want and you can populate your form with it.

    If I understand you correctly, it doesn't sound like you're normalizing your tables, which I highly recommend. Here's an article on it - normalization It's pretty in depth, but if you're new to MySQL, it's definitely good knowledge to have.
    "Those who can make you believe absurdities can make you commit atrocities."
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    Thank you for your fast answer.

    Each table has and Id field. But I d'ont want to look the selection to a specific id

    on my form, the country field is the first one to select a country from
    Organisation field is the second in the form.

    What i need is to pecify in my select, only organization associated with the country already selected in the first field by the user.

    So the query should show only organization with selected country on the form.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    SELECT * FROM table1 WHERE country='$yourcountryvariable'
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    102
    Rep Power
    10
    Originally Posted by Rhytz
    SELECT * FROM table1 WHERE country='$yourcountryvariable'
    If there could be multiple organisations, you would want to specify it as being distinct:

    SELECT DISTINCT organisation FROM table1 WHERE country='$yourcountryvariable'

    Even if there were 10 "hospital" organisations, it would only return one row containing "hospital" (depending on any joining tables, etc).
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    Thank you for your answer.
    That is what I want but it still doesn't work just because I don't know to say in '$yourcountryvariable'
    This country in this form. Basycally I don't know how to taill the = is from a field X located on the current form.
    I use to work with access and with access I will say for exemple
    form!country how do I say that with Mysql within PHP statment?

    SELECT DISTINCT organisation FROM table1 WHERE country='$yourcountryvariable'
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    102
    Rep Power
    10
    Your form should have an action and a method. The method can be GET or POST, which either sends the data via the URL or as posted data. Read this:

    http://www.html-form-guide.com/php-form/php-form-processing.html

    and this

    http://www.johnrockefeller.net/php-securing-your-input-forms-from-mysql-injection-attacks/

IMN logo majestic logo threadwatch logo seochat tools logo