Dave the Golfer Ответов: 1

Как заполнить поле выбора с помощью данных из таблицы mysql


Я хочу заполнить выпадающий список именами, содержащимися в таблице MySQL. Все вроде бы работает, но список всегда на одно имя короче.

Что я уже пробовал:

код формы
<p>Select Player from dropdown list: (Required) <select id="FullName"  name="FullName" 
            <?php 
            playerFullName_load()
            ?>
        >
        </select></p>


код функции:
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);
}

1 Ответов

Рейтинг:
6

Dave the Golfer

Изменил свой код на этот, и теперь он работает правильно!

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


а это
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);
}