我正在尝试使用PHP填充下拉列表,但我继续从选择选项中获取一个空白单元格!任何帮助表示赞赏!
<fieldset id="Amount">
<?php
include ("connect.php");
$db = new mysqli($servername, $username, $password, $dbname);
if(!db) {
exit('Connect Error (' . mysqli_connect_error() . ') '
. mysqli_connect_error());
}
?>
<label><span class="text_9">AMOUNT:</span></label>
<select required name="amount" id="amount">
<option value="">Select</option>
<?php
$stmt = $db->prepare("SELECT DISTINCT 'Amount' FROM 'Contracts'");
$stmt->execute();
$stmt->bind_result($amt);
while ($stmt->fetch_()){
echo "<option value='$amt'></option>";
}
$stmt->close();
?>
</select>
</fieldset>
谢谢!
答案 0 :(得分:0)
尝试类似这样的东西,让html脱离php块
<?php
$stmt = $db->prepare("SELECT DISTINCT 'Amount' FROM 'Contracts'");
$stmt->execute();
$stmt->bind_result($amt);
while ($stmt->fetch_()){
?>
<option value="<?php echo $amt; ?>"> <?php echo $amt; ?> </option>
<?php
}
$stmt->close();
?>
答案 1 :(得分:0)
从表名中删除单引号并将其更改为:
$stmt = $db->prepare("SELECT DISTINCT 'Amount' FROM Contracts");
并确保您的表格中有数据。