2

I am able to populate a select drop down with all entries from the database but stuck where the already set value is not able to show or shows as a duplicate. i searched on here and found two similar questions but their answers did not help me. my code below

connect.php is included which has all the database connect information

<?php
include 'connect.php';
$qry=mysql_query("SELECT * FROM experts", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>


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


<?php
while($row=mysql_fetch_array($qry))
{
echo "<option value='".$row['expert']."'>".$row['expert']."</option>";
}
?>

</select>

the problem with this is it includes a duplicate entry as well as the selected entry. i know i need to add an if statement to remove the selected entry but not sure how.

PHP MySQL Drop Down Box Populate Selected Value seems to have my answer but i don't seem to be able to use the example to suit my code as its worded differently.

Community
  • 1
  • 1
Saud Kazia
  • 89
  • 1
  • 9
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. **They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)**. Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 19 '14 at 19:14
  • 1
    ` – Funk Forty Niner Dec 19 '14 at 19:14
  • Jay. I learned that too late but i have invested far too much time and effort that changing my code to PDO or MySQLi will not be the most efficient thing to do. – Saud Kazia Dec 19 '14 at 19:21
  • fred. i understand that, but if i remove that line it doesn't take the currently selected database value – Saud Kazia Dec 19 '14 at 19:21
  • I find that rather strange. – Funk Forty Niner Dec 19 '14 at 19:28

2 Answers2

1

You just need to remove the <option> that is out of the while loop. That's why your first item is duplicated.

<?php
    include 'connect.php';

    $qry=mysql_query("SELECT * FROM experts", $con);

    if(!$qry)
    {
        die("Query Failed: ". mysql_error());
    }
?>

<select name="expert" id="expert">
<?php
    while($row=mysql_fetch_array($qry))
    {
        echo "<option value='".$row['expert']."'>".$row['expert']."</option>";
    }
?>
</select>
Willz
  • 78
  • 9
  • That's what I said but OP says *"fred. i understand that, but if i remove that line it doesn't take the currently selected database value"*. – Funk Forty Niner Dec 19 '14 at 19:25
  • Yeah, i saw your comment after i posted the answer. Although OP didn't mention the need to get the selected option in the problem description. – Willz Dec 19 '14 at 19:34
1
<select name="expert" id="expert">
    <!-- Show an empty option so the "default" is blank -->
    <option value=""></option>
    <?php
    // Define the desired selected value from the database (if any)
    $selected_value = CODE_TO_GET_CURRENT_VALUE;
    // Loop through all of the available options to create your... well, options
    while($row=mysql_fetch_array($qry))
    {
        // If this option matches the one you want to select, add the HTML "selected" attribute
        $selected = ($row['expert'] == $selected_value ) ? ' selected' : '';
        echo "<option value='" . $row['expert'] . "'" . $selected . ">" . $row['expert'] . "</option>";
    }
    ?>
</select>
mopo922
  • 6,293
  • 3
  • 28
  • 31
  • im gonna try this out. – Saud Kazia Dec 19 '14 at 19:28
  • how do i define the default value. is this when there is none selected or the one already in the database. – Saud Kazia Dec 19 '14 at 19:30
  • That would be a value already in the database. If there's no value in the db, just return `false` or `null` for `$default_value`, then it will never match `$row['expert']` and no options will be marked as `selected`. – mopo922 Dec 19 '14 at 19:32
  • ok i think its working with this code - no duplicates but now i need to include a null object. would i just add a select option outside the loop or is there another way. – Saud Kazia Dec 19 '14 at 19:34
  • excellent works like a charm although i don't understand why it works. maybe you can just quickly break it down for me. i have already marked your answer as correct – Saud Kazia Dec 19 '14 at 19:43
  • @SaudKazia added some comments to try to clear things up. Sorry it wasn't clear in the first place. – mopo922 Dec 19 '14 at 19:48