Jump to content

Problems with getting value from mysql database using php and html dropdown box


DirtySnipe

Recommended Posts

I have a php file which contains a html form. Within the html form are multiple drop down boxes which are supposed to pull information from a mysql database.

 

What im also trying to do is get it to show a default entry from the database.

 

<select style="width:100px" name="person_involved">

<?php
mysql_connect("localhost", "username", "password") or die("Connection Failed");
mysql_select_db("mydatabase") or die("Connection Failed");
$sql = "SELECT * FROM hesk_location ORDER BY name ASC";
$result = hesk_dbQuery($sql);
while ($row=hesk_dbFetchAssoc($result))
{
    $sel = $row['isDefault'] == true ? "selected='selected'" : "";
?>
    <option <?=$sel?> value="<?=$row['name']?>"><?=$row['name']?></option>
<?php
}
?>

      </select>

 

but for some reason it only displays

 

Value="">

 

not the values from the database.. can anyone help me out here?

Does your server have short tags enabled?

Try changing it to

<option <?php echo $sel; ?> value="<?php echo $row['name']; ?>"><?php echo $row['name']; ?></option>

 

 

Or for readability, don't leave PHP.

 

echo '<option '.$sel.' value="'.$row['name'].'">'.$row['name'].'</option>';

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.