douju6542 2011-11-28 17:38
浏览 28
已采纳

使用MySQL值更改HTML DropDown默认值

I'm working on a profile page, where a registered user can update their information. Because the user has already submitted their information, I would like their information from the database to populate my HTML form.

Within PHP, I'm creating the HTML form with the values filled in. However, I've tried creating an IF statement to determine whether an option is selected as the default value. Right now, my website is giving me a default value of the last option, Undeclared. Therefore, I'm not sure if all IF statements are evaluation as true, or if it is simply skipping to selected=selected.

Here is my HTML, which is currently embedded with 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;

?>
  • 写回答

3条回答 默认 最新

  • dozxos6346 2011-11-28 17:48
    关注

    You seem to be missing any tags in your code, which means that nothing is being processed. Something more along the lines of this should be used:

    <option <?php if($row["Major"] == "Accounting"){ echo "selected"; } ?>>Accounting</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)