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条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端