Jump to content

[SOLVED] query from db, add values in dropdown menu


jjmusicpro

Recommended Posts

1. Query the database to get the ids and/or values

2. Open a SELECT control: <SELECT name='something'>

3. Loop over the query results, inserting an OPTION for each one:

<OPTION value='{$row['id']}'>{$row['name']}</OPTION> or similar...

4. Close the select: </SELECT>

5. On POST, check the value of the control: $someval = $_POST['something'];

6. header to whatever page you want to go to: header("location: someotherpage.php");exit;

Link to comment
Share on other sites

Adding in any menu is quite simple.

      ...............................................................

      Just select the information or data from the database.

      Use the <select> tag and put the selected information

      into the <option> tag .

      And your menu will be created.

      its quite simple just try it .

      Reply me if you face any problem. ??? :-[

      .............................................................

Link to comment
Share on other sites

<form action='accounts.php' method='GET'>
<select name='name'>
<?php
parse_str("$QUERY_STRING");
$db = mysql_connect("[your host - usually "localhost"]", "[your username]","[your password]") or die("Could not connect.");
if(!$db) 
die("no db");
if(!mysql_select_db("[your database]",$db))
die("No database selected."); 
$acc1="SELECT * from accounts";
$acc2=mysql_query($acc1) or die("Could not select accounts.");
while($acc3=mysql_fetch_array($acc2)) {
echo "<option value='$query[name]'>$query[name]</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

 

that would connect to a mysql database, select table accounts with one field, name, and print all the rows.

Link to comment
Share on other sites

I tried to run that code and it didnt work :(

It builds the page, and the drop down, but nothings in the drop down :(

 

I have a table called zipcodes, with a column called account_name

 


<form action='nothing.php' method='GET'>
<select name='name'>
<?php
parse_str("$QUERY_STRING");
$db = mysql_connect("[localhost]", "[xxxxxxx]","[xxxxx]") or die("Could not connect.");
if(!$db) 
die("no db");
if(!mysql_select_db("[xxxxxx]",$db))
die("No database selected."); 
$acc1="SELECT * from zipcodes";
$acc2=mysql_query($acc1) or die("Could not select accounts.");
while($acc3=mysql_fetch_array($acc2)) {
echo "<option value='$query[account_name]'>$query[account_name]</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

Link to comment
Share on other sites

$sql = "SELECT * FROM `table`";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 0){
echo "No values in database!\n";
}else {
echo "<select name=\"zip\">\n";
   while($row = mysql_fetch_assoc($res)){
   echo "<option value=\"$row[zip]\">$row[town]</option>\n";
   }
echo "</select>\n";
}

Link to comment
Share on other sites

I tried that but didnt work:

Still gives blank drop down...

 

<form action='nothing.php' method='GET'>
<select name='name'>
<?php
parse_str("$QUERY_STRING");
$db = mysql_connect("[localhost]", "[xxxxx]","[xxxxxxx]") or die("Could not connect.");
if(!$db) 
die("no db");
if(!mysql_select_db("[xxxxxxx]",$db))
die("No database selected."); 
$sql = "SELECT * FROM `zipcodes`";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 0){
echo "No values in database!\n";
}else {
echo "<select name=\"zip\">\n";
   while($row = mysql_fetch_assoc($res)){
   echo "<option value=\"$row[account_name]\">$row[iD]</option>\n";
   }
echo "</select>\n";
}

?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

Link to comment
Share on other sites

I changed it but sill didnt want to work: The page comes up with a dropdown, but nothing is in the drop down:

 

<form action='nothing.php' method='GET'>Select Account:<br>
<select name='name'>
<?php
parse_str("$QUERY_STRING");
$db = mysql_connect("[localhost]", "[xxxxxx]","[xxxxx]") or die("Could not connect.");
if(!$db) 
die("no db");
if(!mysql_select_db("xxxxxxx]",$db))
die("No database selected."); 
$sql = "SELECT * FROM `zipcodes`";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 0){
echo "No values in database!\n";
}else {
echo "<select name=\"zip\">\n";
   while($row = mysql_fetch_assoc($res)){
   echo "<option value=\"$row[account_name]\">$row[search_id]</option>\n";
   }
echo "</select>\n";
}

?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

Link to comment
Share on other sites

<form action='nothing.php' method='GET'>
<select name='name'>
<?php
parse_str("$QUERY_STRING");
$db = mysql_connect("localhost", "username","password") or die("Could not connect.");
if(!$db) 
die("no db");
if(!mysql_select_db("dbname",$db))
die("No database selected."); 
$acc1="SELECT * from zipcodes";
$acc2=mysql_query($acc1) or die("Could not select accounts.");
while($acc3=mysql_fetch_array($acc2)) {
echo "<option value='$acc3[account_name]'>$acc3[search_id]</option>"; // was $query[account_name] - not set, i set $acc3 - this was my old one
}
?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

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.