2011-11-28 92 views
0

我正在处理个人档案页面,其中注册用户可以更新他们的信息。由于用户已经提交了他们的信息,我希望他们从数据库中获取信息来填充我的HTML表单。使用MySQL值更改HTML DropDown默认值

在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; 

?> 

回答

1

你似乎缺少在代码中的任何代码,这意味着没有什么正在处理中。东西沿此线更应使用:

<option <?php if($row["Major"] == "Accounting"){ echo "selected"; } ?>>Accounting</option> 
+0

我的解决方案 – MrJ

+0

目前的轻微变形,整个形式下的PHP代码,所以这就是为什么我的变量$行[大]正在从MYSQL调用。我是否需要将IF语句的嵌套PHP标签工作? –

+0

HTML和PHP是两个完全不同的领域。您不得将HTML放置在PHP标记中,反之亦然。 – FrozenFire

2

您已经混合HTML和PHP不宣而PHP标签,你也用“选择”作为一个属性。 ..

<select name="Major"> 
    <option <?php echo ($row['Major'] == "Accounting") ? " selected" : ""; ?>>Accounting</option> 

    .... 

</select> 

我已经做了第一个,但你可以遵循相同的模式

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> 

您可能会发现一些本读有用的,以帮助解释一些概念