Jump to content

[SOLVED] need help with drop down menu


gordsmash

Recommended Posts

Hello guys i need help with making my drop down menu to work with php.

Here is the code that i am using...

<?php
$sql = "SELECT * FROM members";
$query = mysql_query($sql); ?>

<select name="members" id="members">
<?php while($row = mysql_fetch_array($query)){ ?>
<option> 
<?php echo $row['user_name']; ?>
</option>
<?php } ?>
</select>

 

Now sometimes the code will actually execute giving me the values i want but most of the time it wont execute leaving the drop down menu blank with nothing in it. If someone could tell me what i need to do with my code that would be awesome.

Link to comment
Share on other sites

You may use this and edit the values:

<?php
echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"\">\n"; 
echo "  <select name=\"select\">\n"; 
echo "    <option>1</option>\n"; 
echo "    <option>2</option>\n"; 
echo "    <option>3</option>\n"; 
echo "  </select>\n"; 
echo "</form>\n"; 
echo "</body>\n"; 
echo "</html>\n";
?>

Link to comment
Share on other sites

Now sometimes the code will actually execute giving me the values i want but most of the time it wont execute leaving the drop down menu blank with nothing in it. If someone could tell me what i need to do with my code that would be awesome.

If it works sometimes, the problem is not with that block of code (not that I would do it like that), it must be with something else.

You say that it leaves the drop down menu blank - do you mean that it has various empty values or that it has no elements?

 

Just for reference an incase you are interested, I would do this bit of code something like this:


// get list of members from the database
$sql = "SELECT user_name FROM members";
$res = mysql_query($sql) or die("Error getting members");
$list_members="";
while($row = mysql_fetch_assoc($res)){
  $list_members.='<option value="'.$row["user_name"].'">'.$row["user_name"].'</option>';
}
?>

<select name="members" id="members">
  <?php echo $list_members; ?>
</select>

Link to comment
Share on other sites

Ok i found out what the problem was. Thank to the code cbolson  suggested...

$res = mysql_query($sql) or die("Error getting members");

 

It gave me the "Error getting members".

So i found out it wasnt connecting to the table for some reason and i fixed it.

I had to put...

$sql = "SELECT user_name FROM gs_uploads.members";

 

to fix it.

 

Thanks a lot cbolson for helping me out buddy!!

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.