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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.