我试图让它根据数据库中的$ rad [' brukertype']设置默认值,这可以使得eighter值为1或2但是如果声明似乎没有工作
$sql = "select A.*, Br.Type from Brukere A INNER JOIN Brukertyper Br ON A.Brukertype=Br.Brukertype";
if ($db) {
$res = $db->query($sql);
while ($rad = $res->fetch_assoc()) {
echo("<td><select name='type'> <option value='1'" if ($rad['Brukertype'] == '1') "selected='selected'" > "'Administrator</option> <option value='2'" if ($rad['Brukertype'] == '2') "selected='selected'">"Iskjører</option> </td>");
答案 0 :(得分:1)
您的最后一行会导致错误。试试这个:
echo "<td><select name='type'>
<option value='1'" . ($rad['Brukertype'] == '1' ? "selected='selected'" : '') . " >Administrator</option>
<option value='2'" . ($rad['Brukertype'] == '2' ? "selected='selected'" : '') . " >Iskjører</option>
</td>";