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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Help! Mysql results based on combo box.

    Hi Guys. Hopefully someone can help me with this...New to coding and pretty lost on this.
    I have a Mysql database which is displaying results to my webpage with no problems. However I would like to be able to add a combo box to my webpage that would update the mysql database results based on the combo box selection. For example if Ford is chosen from the combo box, the webpage would refresh and show all the results for Ford in the webpage. Can someone please help me?

    Here is the code I have at the moment that works just fine. But results of the database are based on the WHERE statement.

    PHP Code:
    <?php
    $con 
    mysql_connect("server","database","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
    mysql_select_db("a5525005_cars"$con);

    $result mysql_query("SELECT * FROM `cars` WHERE Makel='Ford'");

    echo 
    "<table class='ex1' border='0' width='113%' style=text-align:center; cellpadding='6' cellspacing='0'>

    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<tr style=font-family:verdana;font-size:80%;>";
      
        echo 
    "<td width=13%>" $row[""] . "<img src=\"" $row["Photo"] . "\"></a>";
        echo 
    '<td width="14%"><a class="mylink" href="' $row['URL'] . '">' $row['Model'] . '</a></td>';    
        echo 
    '<td width="5%"><a class="mylink" href="' $row['URL'] . '">' $row['Year'] . '</a></td>';
        echo 
    '<td width="4%"><a class="mylink" href="' $row['URL'] . '">' $row['Fuel'] . '</a></td>';
           
    echo 
    "</tr>";
      }
    echo 
    "</table>";
    mysql_close($con);
    ?>
    Last edited by amg182; 05-30-2011 at 03:43 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    User input from a form can be made available via the _GET or _POST superglobal, depending on the form method (typically POST). Selection menus are named, and the selected item(s) are sent by the name, so you can retrieve it from $_POST['yourComboName'];. This will be an array if you allow multiple selections.
    This data can then be used in the query's where. Personally I'd write a table of makes so that I can pass around a numerical ID, but if you stick with a string you must use mysql_real_escape_string on it to prevent a SQL injection.

    You won't need to refresh the page beyond this. The where provided by the submit would be used to control the data that the page will display.

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for reply, I have tried playing around based on what you have said, but my lack of knowledge and experience is letting me down...

    I have tried the following but no luck, is this on the right track?

    Combobox name/id = "combobox"
    PHP Code:
    ...................

      }

    mysql_select_db("b5525035_cars"$con);

    $result mysql_query("SELECT * FROM `cars` WHERE Make=$_POST['combobox'];");

    echo 
    "<table>

    ........ 
    Thanks again, I do appreciate it!

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up

    Got it working thanks anyway!


  •  

    Posting Permissions

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