0

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>
3
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by iraberyl
0

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

write like this.

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

it will works fine.

Edited by muralikalpana: n/a

0

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

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

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

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?

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.