尝试创建一个组合框,其中选择了用户的当前值。我认为我的问题在于撇号和引号 - 任何一个有敏锐眼光的人都可以帮忙吗?变量$ MCI在任何引号/撇号之前创建并且运行正常。
$MCI = '';
$MCI = $row['MobileCarrierID'];
echo '
<select name="MobileCarrierName">
<?php
$sql = mysqli_query("SELECT MobileCarrierID, MobileCarrierName FROM tblMobileCarrier ORDER BY MobileCarrierName;");
while ($row = mysqli_fetch_array($sql)){
$MCISelected = (' . $MCI . '==$row["MobileCarrierID"] ? " selected" : "");
echo "<option value=" . $row['MobileCarrierID'] . $MCISelected . ">" . $row['MobileCarrierName'] . "</option>";
}
?>
</select>';
谢谢!
答案 0 :(得分:0)
<?php
$sql = mysqli_query("SELECT MobileCarrierID, MobileCarrierName FROM tblMobileCarrier ORDER BY MobileCarrierName;");
?>
<select name="MobileCarrierName">
<?php
while ($row = mysqli_fetch_array($sql)){
$MCI = $row['MobileCarrierID'];
$MCISelected = ($MCI==$row["MobileCarrierID"]) ? " selected" : "";
echo "<option value=".$MCI." ".$MCISelected.">".$row['MobileCarrierName']."</option>";
}
?>
</select>
尝试这个
答案 1 :(得分:0)
你有
echo '
<select name="MobileCarrierName">
<?php
$sql=
需要更改为
echo '<select name="MobileCarrierName">';
$sql=
另外
$MCISelected = (' . $MCI . '==$row["MobileCarrierID"] ? " selected" : "");
需要更改为
$MCISelected = ($MCI==$row["MobileCarrierID"])? " selected" : "";
你的mysqli_query缺少数据库连接,即
mysqli_query($db,$query);
最后,关闭
echo '</select>';
您的引号和括号已关闭,并且您已将所选变量放在选项中的值中,完整编辑的代码应如下所示......
<?php
echo '<select name="MobileCarrierName">';
$sql = mysqli_query($conn, "SELECT MobileCarrierID, MobileCarrierName FROM tblMobileCarrier ORDER BY MobileCarrierName;");
while ($row = mysqli_fetch_array($sql)){
$MCISelected = ($MCI==$row["MobileCarrierID"])? " selected" : "";
echo '<option '.$MCIselected.' value="'.$row["MobileCarrierID"].'">'.$row["MobileCarrierName"].'</option>';
}
echo'</select>';
查看How to set an option from multiple options or array with different values to views as selected in select box using PHP以获取有关其工作原理的指南