我正在处理个人资料页面,注册用户可以在其中更新他们的信息。由于用户已经提交了他们的信息,我希望他们从数据库中获取信息来填充我的HTML表单。
在PHP中,我正在创建填写了值的HTML表单。但是,我尝试创建一个IF语句来确定是否选择了一个选项作为默认值。现在,我的网站给了我最后一个选项Undeclared的默认值。因此,我不确定所有IF语句的评估是否为真,或者它是否只是跳过选择=选择。
这是我的HTML,目前嵌入了PHP(<?php ?>
):
<?php
// Connect to MySQL
$db = mysql_connect("", "xxx", "xxx");
if (!$db)
{
exit("Error - Could not connect to MySQL");
}
$er = mysql_select_db("cs329e_fall11_nemo008", $db);
if (!$er)
{
exit("Error - Could not select the database");
}
$query = "SELECT *
FROM tblMembers
WHERE Username = 'fzr11017' ";
$result = mysql_query($query);
if (!$result)
{
print("Error - The query could not be executed");
$error = mysql_error();
print("<p>:" . $error . "</p>");
exit;
}
$row = mysql_fetch_array($result);
print <<<FORM
<form id="frmRegister" action="update.php" method="post" onsubmit="return Validate()" >
<table>
<tr>
<th><br /></th>
<td><br /></td>
</tr>
<tr>
<th align="left">Username:</th>
<td><input type="text" name="Username" maxlength="10" value=$row[Username] readonly="readonly"/></td>
</tr>
<tr>
<th align="left">First Name:</th>
<td><input type="text" name="FirstName" value=$row[FirstName] readonly="readonly" /></td>
</tr>
<tr>
<th align="left">Last Name:</th>
<td><input type="text" name="LastName" value=$row[LastName] readonly="readonly" /></td>
</tr>
<tr>
<th align="left">Email Address:</th>
<td><input type="text" name="Email" value=$row[Email] /></td>
</tr>
<tr>
<th align="left">Phone Number:</th>
<td><input type="text" name="Phone" maxlength="10" value=$row[Phone] /></td>
</tr>
<tr>
<th align="left">Year:</th>
<td>
<select name="Year" >
<option if(strcmp($row[Year], 'Freshman') == 0){ selected="selected"} >Freshman</option>
<option if(strcmp($row[Year], 'Sophomore') == 0){ selected="selected"} >Sophomore</option>
<option if(strcmp($row[Year], 'Junior') == 0){ selected="selected"} >Junior</option>
<option if(strcmp($row[Year], 'Senior') == 0){ selected="selected"} >Senior</option>
</select>
</td>
</tr>
<tr>
<th align="left">Primary Major:</th>
<td>
<select name="Major">
<option if($row[Major] == Accounting){ selected="selected"}>Accounting</option>
<option if($row[Major] == Business Honors Program){ selected="selected"}>Business Honors Program</option>
<option if($row[Major] == Engineering Route to Business){ selected="selected"}>Engineering Route to Business</option>
<option if($row[Major] == Finance){ selected="selected"}>Finance</option>
<option if($row[Major] == International Business){ selected="selected"}>International Business</option>
<option if($row[Major] == Management){ selected="selected"}>Management</option>
<option if($row[Major] == Management Information Systems){ selected="selected"}>Management Information Systems</option>
<option if($row[Major] == Marketing){ selected="selected"}>Marketing</option>
<option if($row[Major] == MPA){ selected="selected"}>MPA</option>
<option if($row[Major] == Supply Chain Management){ selected="selected"}>Supply Chain Management</option>
<option if($row[Major] == Undeclared){ selected="selected"}>Undeclared</option>
</select>
</td>
</tr>
<tr>
<th><br /></th>
<td><br /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="btnSubmit" value="Submit" /></td>
<td align="center"><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
FORM;
?>
答案 0 :(得分:2)
你混合了HTML和PHP而没有声明PHP标签,你也使用'selected'作为属性...
<select name="Major">
<option <?php echo ($row['Major'] == "Accounting") ? " selected" : ""; ?>>Accounting</option>
....
</select>
我已经完成了第一个,但你可以遵循相同的模式
答案 1 :(得分:2)
该代码看起来可以使用towel:
<select name="Major">
<?php
$options = array(
'Accounting'
, 'Business Honors Program'
, 'Engineering Route to Business'
, 'Finance'
, 'International Business'
, 'Management'
, 'Management Information Systems'
, 'Marketing'
, 'MPA'
, 'Supply Chain Management'
, 'Undeclared'
);
foreach( $options as $option )
{
printf(
"<option%s>%s</option>\n"
, ($row['Major'] == $option ? ' selected="selected"' : '')
, htmlentities($option)
);
}
?>
</select>
您可能会发现这些阅读有助于解释上面使用的一些概念:
答案 2 :(得分:1)
您似乎缺少代码中的任何标记,这意味着没有任何标记正在处理中。应该使用更多内容:
<option <?php if($row["Major"] == "Accounting"){ echo "selected"; } ?>>Accounting</option>