Menu

Getting data from MySQL into dropdownlist

Help
Ned
2004-10-06
2013-06-03
  • Ned

    Ned - 2004-10-06

    Hi All

    Ive found phpformgenerator really usefull but was wondering is it possible to link the data in a dropdown list to current information in MySQL

    ie.
    i have a table in MySQL with a car model row that contains all the list of cars that we supply, can i put a dropdown list on my form that grabs the info from the car model table and only allows the user to select from the available models.

    It just that this row has thousands of unique rows and would be very useful if it was possible, ive been using phpmyedit which does this but isnt as flexible as phpformgenerator

    Cheers

    Ned
    ned@kelly.org.au

     
    • Stephen Hale

      Stephen Hale - 2004-10-06

      As of now there is no current way to do this using phpformgenerator, but thank you for brining it to our attention. I am working on the next version of phpformgenerator and this does sound like a good feature to include. I will check out phpmyedit and see how they implement this feature. Any other features needed, please feel free to let us know.

      Steve
      --------------
      Microterra Technical Staff

       
    • Stephen Hale

      Stephen Hale - 2004-10-06

      Okay, after reviewing phpmyedit, these are two different types of applications. phpmyedit allows you to edit mysql databases. phpFormGenerator allows you to create forms for your website along with a processor to store the information, handle errors, and redirect users to a thank you page. Your post does give me ideas for the next version of the application though.

      As always, I am only human, perhaps I missed something about phpmyedit, seeing as it was the first time I've used the script. If so, please feel free to correct me.

      Steve
      ----------------
      Microterra Technical Staff

       
    • Ash

      Ash - 2005-04-25

      Terrific Program!!!!  Thanks to all the developers and contributors.  Here is my suggestion to populate a dropdown selection with data from another table.  Please realize I am a rank amateur so there may be a better way to do this...

      First, give your form a .php (rather than .html) extension so we can call code in it.

      Then, in your form, connect to the database from wich you want to get the values.  I do this by putting the following code in the head of the form:
      *****************

      <?php
      include("connect.php");
      ?>

      *****************
      If you don't have a connection script like the one mentioned above (connect.php), you can make one by pasting the following code into a blank page, naming it connect.php, and putting it in your form's directory.  Change the values to reflect your database information.
      *****************

      <?php
      $MySqlHostname = "localhost";
      $MySqlUsername = "database_user";
      $MySqlPassword = "user_password";
      $MySqlDatabase = "database_name";
      $ServerConnect = mysql_connect("$MySqlHostname","$MySqlUsername","$MySqlPassword")or die ("Problem:Couldn't connect to MySQL Server.");
      $DatabaseConnection = @mysql_select_db("$MySqlDatabase", $ServerConnect)or die("Problem: Couldn't select the $MySqlDatabase Database.");
      ?>

      *****************
      Now back to the form....  Place this code also in the head of your form and change the variables accoringly.  All the places to change are marked between *s.  Remove the *s as you enter the proper values.
      *****************

      <?php
      function generate_box() {
          $sql = "SELECT * FROM *table_name*";
          $result = mysql_query($sql);
          $entries = mysql_num_rows($result);
          for($n = 0; $n <= $entries; $n++) {
              $sql = "SELECT * FROM *table_name* WHERE *table_id* = $n";
              $result = mysql_query($sql);
              while ($info = mysql_fetch_array($result)) {
                  foreach( $info AS $key => $val ) {
                      $$key = stripslashes( $val );
                  }
                 $options .= "<option value=\"$*column_value_to_enter*\">$*column_value_to_show*</option>";
                }
          }
          return $options;
      }
      $options = generate_box();
      ?>

      *****************
      Finally, replace the appropriate field in body the form with:
      *****************

      <tr><td> *Form_field_name*</td>
      <td>
      <table cellspacing="0" cellpadding="0" border="0">
          <tr>
                      <td><span>
      <select name="* field_on_this_form*">
      <? echo "$options"; ?>
      <option value='*field_on_this_form*'></span></td>
          </tr>
      </table></td></tr>

      *****************
      This may seem confusing, but I assume anyone looking to do this will be able to figure it out from here.  Also, I would be happy to help anyone if needed.

      Ash

       

Log in to post a comment.