Jump to content

[SOLVED] Placing Mysql into drop down form


whelpton

Recommended Posts

Hi guys,

 

I was wondering weather it would be possible to take the contents of a mysql table and insert it into a drop down form.

 

My mysql table is called "Users" and I want to take the row "Username" and output it so that it displays all the current usernames in a form field such as:

 

<select name="select" id="select">
      <option>USERNAME1</option>
      <option>USERNAME2</option>
    </select>

 

Obviously replacing the static username 1 and username 2 with the content from the mysql table

Would it look something like this:

<select class="input2" name="username">
</option><?
	$query = "SELECT * FROM user ORDER BY username ASC";
	$result = @mysql_query($query);
	if ($result){
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo '<option value="'.$row['username'].'"';
		if ($_POST['username'] = $row['username']){echo 'selected="selected"';}
		echo '>'.$row['username'].'</option>';}}
	?></select>

 

I actually totally made this for something that I made a while back. Adjust it accordingly... If it works for you at all... I'm kind of a noob.

Would it look something like this:

<select class="input2" name="username">
</option><?
	$query = "SELECT * FROM user ORDER BY username ASC";
	$result = @mysql_query($query);
	if ($result){
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo '<option value="'.$row['username'].'"';
		if ($_POST['username'] = $row['username']){echo 'selected="selected"';}
		echo '>'.$row['username'].'</option>';}}
	?></select>

 

I actually totally made this for something that I made a while back. Adjust it accordingly... If it works for you at all... I'm kind of a noob.

 

That should work ok. Just use '<?php' instead of '<?'. Because you could get errors if short tags are not on in php.ini

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.