Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to populate a dropdown list with the names held in a MySQL table. All seems to work but the list is always one name short.

What I have tried:

Form Code
<p>Select Player from dropdown list: (Required) <select id="FullName"  name="FullName" 
            <?php 
            playerFullName_load()
            ?>
        >
        </select></p>


Function Code:
function playerFullName_load(){
    require '../../configure.php';
        $uName = "";
     $Surname = "";
     $FullName = "";
    $ID = "";
$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$db_found = mysqli_select_db($db_handle, DB_NAME);
// Check connection
if (!$db_found) {
    die("Connection failed: " . mysqli_connect_error());
}
if ($db_found) {
   
$SQL = "SELECT * FROM `snravalonplayerstb` ORDER BY `FirstName` ASC, `Surname` ASC";
$result = mysqli_query($db_handle, $SQL);
while ( $db_field = mysqli_fetch_assoc($result) ) {
    $uName = $db_field['FirstName'];
    $Surname = $db_field['Surname'];
    $_SESSION['TelephoneNo'] = $db_field['TeleNo'];
    $FullName = $uName." ". $Surname;
    echo "<option value='$FullName'> $FullName </option>";
}
echo "<option disabled selected value> -- select an option -- </option>";
}
else {
    print "Database NOT Found ";
}
mysqli_close($db_handle);
}
Posted
Updated 18-Nov-18 11:44am

1 solution

Changed my code to this and it now works correctly!

<p>Select Player from dropdown list: (Required) <select id="FullName"  name="FullName" 
            <?php 
            playerFullName_load()
            ?>
            >
            <option disabled selected value> -- select the player -- </option>
        </select></p>


and this
function playerFullName_load(){
    require '../../configure.php';
        $uName = "";
     $Surname = "";
     $fullName = " ";
    $ID = "";
$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$db_found = mysqli_select_db($db_handle, DB_NAME);
// Check connection
if (!$db_found) {
    die("Connection failed: " . mysqli_connect_error());
}
if ($db_found) {
    
    $SQL = "SELECT * FROM `snravalonplayerstb` ORDER BY `FirstName` ASC, `Surname` ASC";
    $result = mysqli_query($db_handle, $SQL);
    if ($ufullName==" "){}
    while ( $db_field = mysqli_fetch_assoc($result) ) {
        $uName = $db_field['FirstName'];
        $uName = trim($uName);
        $Surname = $db_field['Surname'];
        $Surname = trim($Surname);
        $fullName = $uName." ".$Surname;
        $selected = '';
        echo "<option value='$fullName' $selected> $fullName </option>";
    }
}
else {
    print "Database NOT Found ";
}
mysqli_close($db_handle);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900