Jump to content

[SOLVED] Mysql and select menu


pcw

Recommended Posts

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

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>";
}
?>

<?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.

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.