I want to populate a dropdownlist from a database using PHP.My table name is msg_people_contact and column to be inserted into is primary_email.I have a code and it connects to the database but the data is not shown in the dropdownlist.Can anyone help me with this code?

<td><label>
     <select>

<?php 
$sql="SELECT primary_email FROM msg_people_contact";

$result =mysql_query($sql);
while ($data=mysql_fetch_array($result)){
?>
<option value ="<?php echo $data['primary_email'] ?>"</option>
<?php } ?>
</select>
    </label></td>
  </tr>

<option value ="<?php echo $data ?>"</option>

write like this.

<option value ="<?=$data[primary_email] ?>">$data[primary_email]</option>

it will works fine.

On line 9, do not close the php yet, try this;

echo "<option value='$data[primary_email]'>$data[primary_email]</option>\n";

By the way, you should first open the connection:

mysql_connect("localhost", "user_name", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

$sql="SELECT primary_email FROM msg_people_contact WHERE primary_email = '".$_REQUEST['primary_email']."';";

$result =mysql_query($sql);

while ($data=mysql_fetch_array($result)){
echo "<option value='$data[primary_email]'>$data[primary_email]</option>\n";
}
?>

write like this.

<option value ="<?=$data[primary_email] ?>">$data[primary_email]</option>

it will works fine.

Thnx,but although now it is populated it shows "$data[primary_email] " inside the dropdownlist.Can you fix it?

Thnx, it worked fine

On line 9, try to remove the ?> tag then put this:

echo "<option value='$data[primary_email]'>$data[primary_email]</option>\n";

Then follow this with an end brace } before closing the php ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.