我在使用HTML / PHP表时遇到问题我正在尝试填充html选择标记 使用数据库中的数据并将其显示在表中。但是具有数据库数据的select标记仅显示在表的第一行中。 这是我的代码:
$result2 = mysqli_query($con,"SELECT vozac FROM vozaci");
$result = mysqli_query($con,"SELECT registracija,tip,status,vozac,napomena2,IDV FROM status_vozila order by status desc")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['registracija'].'</td>';
echo '<td>'.$row['tip'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>
<select name="statusi">
<option value="Na parkingu" name="Na parkingu">Na parkingu</option>
<option value="Na turi" name="Na turi">Na turi</option>
<option value="Potreban radionici" name="Potreban radionici">Potreban radionici</option>
<option value="Potreban radionici-na parkingu" name="Potreban radionici-na parkingu">Potreban radionici-na parkingu</option>
<option value="Potreban radionici-na turi" name="Potreban radionici-na putu">Potreban radionici-na turi</option>
<option value="Razvoz" name="Razvoz">Razvoz</option>
</select>
<input type="submit" name="update" value="Promeni" /></td>';
echo '<td>'.$row['vozac'].'</td>';
echo '<td>';
echo '<select name="list">';
while ($row = mysqli_fetch_array($result2)) {
echo '<option value="' . $row['vozac'] .'">' . $row['vozac'] .'</option>';
}
echo '</select>';
echo '</td>';
echo '<td>'.$row['napomena2'].'</td>';
echo '<td><input type="text" name="napomena2" value=""><input type="submit" name="update3" value="Dodaj" /></td>';
echo'<td><input type="hidden" name="IDV" value="'.$row['IDV'].'"></td>';
echo '</tr>';
echo '</form>';
}
mysqli_close($con);
?>
答案 0 :(得分:1)
您正在覆盖row
变量。在其他循环更改变量名称
while ($row1 = mysqli_fetch_array($result2)) {
echo '<option value="' . $row1['vozac'] .'">' . $row1['vozac'] .'</option>';
}
此外,您需要先将所有值存储在一个数组中,然后遍历它。而不是多次调用相同的查询。获取数据后。在您再次调用(mysqli_query()
)查询之前,它不会再次执行。
修改强>
我已经提到首先将所有值存储在一个数组中,然后遍历它。试试这段代码:
$result2 = mysqli_query($con,"SELECT vozac FROM vozaci");
$list_vozaci = array();
while ($row = mysqli_fetch_array($result2)) {
$list_vozaci[] = $row['vozac'];
}
$result = mysqli_query($con,"SELECT registracija,tip,status,vozac,napomena2,IDV FROM status_vozila order by status desc")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['registracija'].'</td>';
echo '<td>'.$row['tip'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>
<select name="statusi">
<option value="Na parkingu" name="Na parkingu">Na parkingu</option>
<option value="Na turi" name="Na turi">Na turi</option>
<option value="Potreban radionici" name="Potreban radionici">Potreban radionici</option>
<option value="Potreban radionici-na parkingu" name="Potreban radionici-na parkingu">Potreban radionici-na parkingu</option>
<option value="Potreban radionici-na turi" name="Potreban radionici-na putu">Potreban radionici-na turi</option>
<option value="Razvoz" name="Razvoz">Razvoz</option>
</select>
<input type="submit" name="update" value="Promeni" /></td>';
echo '<td>'.$row['vozac'].'</td>';
echo '<td>';
echo '<select name="list">';
foreach ($list_vozaci as $key => $value) {
echo '<option value="' . $value .'">' . $value .'</option>';
}
echo '</select>';
echo '</td>';
echo '<td>'.$row['napomena2'].'</td>';
echo '<td><input type="text" name="napomena2" value=""><input type="submit" name="update3" value="Dodaj" /></td>';
echo'<td><input type="hidden" name="IDV" value="'.$row['IDV'].'"></td>';
echo '</tr>';
echo '</form>';
}
mysqli_close($con);
?>