我会尽量缩短这个。很可能我遗漏了一些明显的东西,所以任何输入都将不胜感激。
SQL 结构/列:
id_user
(首要的关键)income_input
income_select
PHP代码:
<?php
$con = mysql_connect("localhost","dbname","pwd");
if (!$con)
{
die('Could not connect to the database: ' . mysql_error());
}
mysql_select_db("dbname", $con);
global $user; // Drupal 7 global
$userid = $user->uid; // Drupal 7 currently logged-in user id
$result = mysql_query("SELECT * FROM my_table WHERE id_user='$userid'");
if($result === FALSE) {
die(mysql_error());
}
if ($user->uid) {
?>
<form>
<label for="edit-1">Label</label>
<input id="edit-1" maxlength="60" size="60" type="text" value="<?php while($row = mysql_fetch_array($result)){ echo $row['income_input']; } ?>">
<select id="select-edit-1">
<option value="daily" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
<option value="weekly" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>
</select>
</form>
<?php
}
mysql_close($con);
?>
输入值 ( income_select
) 已从数据库中正确获取,但下拉列表 ( income_select
) 不会返回选定状态。如果我将整个表单包含在 WHILE 语句中,它工作正常,我可以快速回显 DB 值,并且选择下拉菜单“选定”状态也工作正常。我会选择这种方法,但我必须保持表单原样,并且只有在当前登录用户有任何条目时才使用数据库中的数据填充它。
我希望以上是有道理的,最后提前感谢您的帮助!