无法填充所有下拉框,只有第一个从 PHP 中的数据库中填充

Unable to populate all drop down boxes, only first is populated from database in PHP

这是我的代码,只有第一个下拉框正在填充。我可以重复下拉框,但无法填充所有下拉框。请帮助我...

$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);

echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){    
    echo "<tr>";
    echo "<td>".$row['Driver_name']."</td>";?>
    <td><select>
<?php while($row1=mysqli_fetch_assoc($query1)){  
    $st=$row1['d_status'];
    echo "<option value='$st'>$st</option>";
    }?>
    </select></td>
<?php
    echo "</tr>";      
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";

我可以重复下拉框但无法填充所有下拉框。请帮助我...

我想将数据库中的数据显示到所有下拉框并保存所选值。我使用的提交按钮不起作用请告诉我为什么?

试试这个它会起作用:

$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);

echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
do {    
    echo "<tr>";
    echo "<td>".$row['Driver_name']."</td>";?>
    <td><select>
<?php do{  
    $st=$row1['d_status'];
    echo "<option value='$st'>$st</option>";
    }while($row1=mysqli_fetch_array($query1)); ?>
    </select></td>
<?php
    echo "</tr>";      
}while($row=mysqli_fetch_array($query));
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";

我对您的代码进行了一些重大更改。在下面的代码中,我将一些变量重命名为一些更易读的名称,这样你就不会那么纠结了。

我将检索状态的查询结果保存到一个变量中,您可以轻松地在 foreach 循环中重复使用该变量。我还编辑了 html 部分并使其完全 PHP 为您节省了一些带有 PHP 的开始和结束标签。

如果您怀疑从查询中返回的值与您期望的值不完全相同,请先在 PHPmyadmin 中试用它们,这样您就知道这不是 PHP 语法或格式化把它们搞砸了。

我还给 select 元素起了一个名字,这样你就可以 link 在 PHP 中传递的值。您可以使用 print_r($_POST); In driver_status.php 来检查通过了哪些值。

<?php
$query_driver = 'select Driver_name from driver_master';
$result_driver = mysqli_query($conn, $query_driver);
$query_statuses ='select d_status from status';
$result_statuses = mysqli_query($conn, $query_statuses);
while($row_status = mysqli_fetch_assoc($result_statuses)){
    $statuses[] = $row_status['d_status'];
}

// $statuses now contains all statuses from the query this saves extra unnecessary queries.

echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
    echo "<tr>";
    echo "<th>Driver Name</th>";
    echo "<th>Status</th>";
    echo "<th>From</th>";
    echo "<th>To</th>";
echo "</tr>";
while($row_driver = mysqli_fetch_assoc($result_driver)){   
    echo "<tr>";
        echo "<td>".$row_driver['Driver_name']."</td>";
        echo "<td><select name='drivers[". $row_driver['Driver_name'] ."][status]'>";
        foreach($statuses as $status){
            echo "<option value='". $status . "'>". $status . "</option>";
        }
        echo "</select>";
        echo "</td>";
    echo "</tr>";      
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";