drop down menu PLEASE HELP ME!!!

Help
kate
2012-04-26
2012-09-19
  • kate
    kate
    2012-04-26

    form = cgi.FieldStorage()

    my_usr_mutation=form.value

    db = MySQLdb.connect(host="127.0.0.1",user="root",passwd="",db="houses")

    cursor = db.cursor()

    cursor.execute("SELECT * FROM HOME")

    rows = cursor.fetchall()

    for row in rows:

    print row

    db.close()

    the above is my .py for querying my database set up in xampp with phpadmin
    sql! i have a drop down menu in my html and this cgi returns all the
    information from my "Home" table. I want to my usr choice to correspond with
    the row in my database and not to return all the info only what the user is
    looking for. lets say my drop down menu is home1,home2.home3 and usr selects
    home1 how do i code that just the information with home1 returns??

    please help!!

     
  • Andy Dustman
    Andy Dustman
    2012-04-27

    SELECT home1 FROM HOME

    ?

     
  • kate
    kate
    2012-04-27

    yes sorry looking at my comment -not much sense made! basically I have a html
    page with a drop down list. I then created my phpadmin database (xampp-sql)
    and populated the database. so lets say you go to my webpage and there is a
    drop down list of choices say: house1,house2,house3 and as a user you select
    on one of these options(lets say house2) and you get back information just
    regarding house2(this information is what I have set up on the database-lets
    say on a table called HOME)

    Then I set up my cgi. and from my cgi I cal call all the info on HOME table
    but what I want is to correspond each option to the user with the row of
    information the user choice is link to. so for each option on my drop down
    menu I want a cgi to assign each option with the specific information(home1 is
    a row on the HOME table and if the user selects home1 I want it to return
    information regarding only home1 so essentially I will be returning a row of
    information)

    ?????? reallystuck.com!!! and would appreciate help!!

     
  • Cat slave
    Cat slave
    2012-04-27

    Not sure if I inderstood right, but there's some info missing here. Isn't it
    just basic SQL?

    cursor.execute('''SELECT * FROM `HOMES` WHERE your_column = %s''', my_usr_mutation)
    
     
  • kate
    kate
    2012-04-30

    hi thanks for your help - it turns out that the problem was with the phpadmin
    database...thanks again.

    however I do have another question. On my html I have multiple drop down
    menus. the user must choose the two options from two drop down menus and then
    click on 'submit'.

    so lets say the html is : choice 1: houseA>houseB>houseC(this is the list)
    choice 2: Red>Blue>green

    user must select an option from both lists and then click submit to get
    information back.

    what would be the sql command for this??

    in the cgi would I first assign the names : my_usr_choice=form.value ????

     
  • Cat slave
    Cat slave
    2012-04-30

    You would need to extract both values from your form, something like:

    house = form['House'].value
    colour = form['Colour'].value
    

    and they call with something like:

    cursor.execute('''SELECT * FROM `HOMES` WHERE your_column1 = %s AND your_column2 = %s''', (house, colour))
    

    Perhaps, have a look at the API: http://mysql-
    python.sourceforge.net/MySQLdb-1.2.2/

     
  • kate
    kate
    2012-04-30

    thats great thanks you !!

     
  • kate
    kate
    2012-04-30

    would you know an SQL statement to search a whole database?

    il create a simple search box in html and then connect it to a cgi with an sql
    statement to search all of my database in phpadmin depending on what the user
    is looking for. if the user types in house1 and clicks search they will get a
    return on all info with house1 from the database??

     
  • Cat slave
    Cat slave
    2012-04-30

    This is getting out of the scope of using MySQLdb. I would suggest you to
    learn SQL first.