PHP, SQL, Drop Down Box?

Alone

Diamond Member
Nov 19, 2006
7,490
0
0
I'm trying to make a drop down box that will list the names in a database I already have. When a name is selected, I'd like to change the variable $name to what was selected. I'm not sure how to do this.

Any ideas?

Forgive the ugly code..

<form action="">
<select onchange="">
<option value="">Choose a name</option>

<?php
$username="name";
$password="password";
$database="db_tracker";

mysql_connect(localhost,$username,$password);

$query = "SELECT name FROM members";
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$n=mysql_result($result,$i,"name");
echo "<option value=" .$n. ">" .$n. "</option>";
$i++;
}
?>
</select>
</form>

I'd like not to SUBMIT the form right after, as the user will have the option to add a number in a field immediately afterwards.

Basically I'd like a user to be able to select his name from a list and enter a number. I'm not worried about authentication or anything.
 
Last edited:

Ka0t1x

Golden Member
Jan 23, 2004
1,724
0
71
Are the records already stored in the database? IE: Are you pulling up an already stored record? If so, just check it vs the value of your option.


echo "<option value='" .$n."'"; if($n==$value) echo "selected"; echo ">" .$n. "</option>";


Just some pointers, unless the data is already stored with the NAME.. I would use the ID from the table and use the ID as the value for the option. Also, using mysql_fetch_array for something like this is much easier than using the for-loop structure.

Code:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo $row['ID']." - ".$row['NAME']."<BR />";
}
 
Last edited: