database help (mySQL, dropdown menu)

Discussion in 'MySQL' started by Tehy, Oct 12, 2007.

  1. #1
    I have mySQL database called phones and I have created table

    
    +-----------+-------+-------+ 
    | Phone     |design | weight| 
    +-----------+-------+-------+ 
    | S500i     |slider |  90 g | 
    | P990i     |bar    | 120 g | 
    | M600i     |bar    | 100 g | 
    +-----------+-------+-------+
    
    Code (markup):
    then I have a dropdown menu:

    <FORM NAME="myform1">
    <SELECT NAME="mylist1">
    <OPTION VALUE="s500i">Sony Ericsson S500i
    <OPTION VALUE="p990i">Sony Ericsson P990i
    <OPTION VALUE="m600i">Sony Ericsson M600i
    </SELECT>
    </FORM>

    So, what I want to do is that when user for instanse selects "Sony Ericsson S500i" from the dropdown menu I want get the data from my mySQL database and show it.

    I'm new with php and mySQL but if I can have some help, I'm sure I can do this :)

    What is the best way to do this?
     
    Tehy, Oct 12, 2007 IP
  2. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i'm trying to learn php and mysql, so, this may not be the best answer. (would also like critique by someone if this does not work at all).

    i think first you need to grab the value for the mysql db
    $mylist1=$_POST['mylist1'];
    Code (markup):
    after you get the submitted answer (i assume there is a submit button? or are you trying to get this with AJAX? if so, i didnt read anything about php+ajax yet, so i can't help you there)
    select the key from the database - also you dont have your table name specified
    $info = mysql_query("SELECT * FROM Phone table_name WHERE Phone='$mylist1'"); 
    if (mysql_num_rows($info) > 0) {
    echo "<table cellpadding=10 border=1>";
    while ($row = mysql_fetch_assoc($info)) {
    echo "<tr>";
    echo "<td>".$info['Phone']."</td>;
    echo "<td>".$info['design']."</td>;
    echo "<td>".$info['weight']."</td>;
    echo "</tr>";
    }
    Code (markup):
    unsure how that will work :\
     
    Lordy, Oct 14, 2007 IP
  3. ste.richards

    ste.richards Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You have several options. Firstly, and my favourite :), would be to use AJAX and have the data displayed from the database without a page reload.

    The easiest way would be to have a PHP if condition.

    EG:

    if($_POST) {

    $query = mysql_query("SELECT * FROM phones WHERE Phone = '{$_POST['mylist1']}'");

    while($r = mysql_fetch_array($query)) {
    echo $r['Phone']." ".$r['design']." ".$r['weight']."<br>";
    }

    }

    Regards,
     
    ste.richards, Oct 14, 2007 IP
  4. Tehy

    Tehy Peon

    Messages:
    254
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks! These helped me to get started! I'll let you know if I need more help :)
     
    Tehy, Oct 14, 2007 IP