Jump to content

SQL Query Dropdown List


mattverg7

Recommended Posts

Hey,

 

I have the following coding:

<?

$dbuser="*******";

$dbpass="*******";

$dbname="virtuda_db";  //the name of the database

$chandle = mysql_connect("localhost", $dbuser, $dbpass)

    or die("Connection Failure to Database");

mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);

 

$mainsection="license";

 

$query1="select name from license";

$result = mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);  //do the query

while($thisrow=mysql_fetch_row($result))

{

  $i=0;

  while ($i < mysql_num_fields($result))

  {

    $field_name=mysql_fetch_field($result, $i);

    echo $thisrow[$i] . " ";  //Display all the fields on one line

    $i++;

  }

echo "<br>";  //put a break after each database entry

}

?>

 

How would I set up this so that instead of just "listing" them out on new lines, it would list the results into a drop down list? Thanks!

Link to comment
Share on other sites

You should really be more explicit in your usage of database results.

<?php
$dbuser="*******";
$dbpass="*******";
$dbname="virtuda_db";  //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass) 
    or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);

$mainsection="license";

$query="SELECT name FROM license";
$result = mysql_db_query($dbname, $query) or die("Failed Query of " . $query1);  //do the query

$optionList = '';
while($row=mysql_fetch_assoc($result))
{
    $optionList .= "<option value=\"{$row['name']}\">{$row['name']}</option>\n";
}

echo "<select name=\"somename\">\n{$optionList}\n</select>";

?>

Link to comment
Share on other sites

You are awesome. Appreciate it.

 

Now my other curiosity is this. With this drop down, what I have in mind, is to have a person select an option from that drop down and be able to hit a button which says "Modify" right next to the drop down. At this point it will query all information from the specified row that "name" corresponds with from the drop down.

 

How would I incorporate this drop down and a modify button from a form that queries all info from that row with that specified name that is selected from the drop down?

Link to comment
Share on other sites

There are a lot of pieces to that puzzle - none of which are difficult, but trying to explain them - in detail - here would be laborious. Here is an outline of what you need to do:

 

1. Create a form in the page where you have the select list The db records really should have a unique ID field to use as the value instead of the name. You can use the name but then you need to add additionally handing to prevent duplicates.

2. Create a prage to receive the form post. That page will take the selected item and query for the record's data. Then create a form inputting the current data. The user can then make change and submit the changes.

3. Create another page to handle the submission of the change.

 

You can of course create one big page to handle all the activities, but I find creating separate pages based upon functionality is better - although I may use one page that all the pages submit to and just include the necessary pages based upon the actions taking place.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.