pcw Posted March 15, 2009 Share Posted March 15, 2009 Hi, I am trying to get a list of usernames from the database and display them in a dropdown menu. When a username is selected I want the value of that username to be the email address registered to it. I cant get the dropdown list to populate and cant see what I am doing wrong. Here is what I have got so far: <?php mysql_connect("$hostname", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db("$db") or die(mysql_error()); $query = "SELECT * FROM profiles WHERE username = 'username' AND 'email' = '$_POST[email]'"; $result=mysql_query($db); echo "<select name=\"email\">\n"; while($username = mysql_fetch_array($result)){ print "<option value='$_POST[email]'>";echo "'$_POST[username]'"; print "</option>"; print "</select>"; } ?> Any help is much appreciated Thanks Link to comment https://forums.phpfreaks.com/topic/149562-solved-mysql-and-select-menu/ Share on other sites More sharing options...
cooldude832 Posted March 15, 2009 Share Posted March 15, 2009 try (a few mistakes here or there on yours, and the logic seems off still) <?php mysql_connect("$hostname", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db("$db") or die(mysql_error()); $query = "SELECT email, username FROM `profiles` WHERE username = 'username' AND 'email' = '".mysql_real_escape_string($_POST[email])."'"; #this line was wrong $result=mysql_query($query) or die(mysql_error()."<br /><br />".$query); if(mysql_num_rows($result) >0){ echo "<select name=\"email\">\n"; while($row = mysql_fetch_array($result)){ echo "<option value='".$row['email']."' "; if($_POST['email'] == $row['email']){ echo "selected='selected' "; } echo ">".$row['username']."</option>\n"; } echo "</select>"; } else{ echo "<b>Error No users found.</b>"; } ?> Link to comment https://forums.phpfreaks.com/topic/149562-solved-mysql-and-select-menu/#findComment-785407 Share on other sites More sharing options...
pcw Posted March 15, 2009 Author Share Posted March 15, 2009 <?php mysql_connect("$hostname", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db("$db") or die(mysql_error()); $query = "SELECT email, username FROM profiles"; $result=mysql_query($query) or die(mysql_error()."<br /><br />".$query); echo "<select name=\"email\">\n"; while($row = mysql_fetch_array($result)){ echo "<option></option>"; echo "<option value='".$row['email']."' "; if($_POST['email'] == $row['email']){ echo "selected='selected' "; } echo ">".$row['username']."</option>\n"; } echo "</select>"; ?> Thanks for your reply, I made a few changes and the above worked. Link to comment https://forums.phpfreaks.com/topic/149562-solved-mysql-and-select-menu/#findComment-785437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.