0

我会尽量缩短这个。很可能我遗漏了一些明显的东西,所以任何输入都将不胜感激。

SQL 结构/列:

  1. id_user(首要的关键)
  2. income_input
  3. 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 值,并且选择下拉菜单“选定”状态也工作正常。我会选择这种方法,但我必须保持表单原样,并且只有在当前登录用户有任何条目时才使用数据库中的数据填充它。

我希望以上是有道理的,最后提前感谢您的帮助!

4

3 回答 3

1

如果您只想获取一行,则不需要一段时间。

首先,获取行并获取应选择的选项。

<?php 

// better use 'fetch_assoc' here
$row = mysql_fetch_assoc($result) or die(mysql_error());

$income_select = $row['income_select'];

$daily = ($income_select=='daily');
$weekly = ($income_select=='weekly');

?>

在形式上,像这样使用它:

<option value="daily" <?php if($daily) echo 'selected'; ?>>Daily</option>
<option value="weekly" <?php if($weekly) echo 'selected'; ?>>Weekly</option>

如果您的 PHP 设置和版本允许,您甚至可以使用它:

<option value="daily" <?= $daily ? 'selected' : '' ?>>Daily</option>
<option value="weekly" <?= $weekly ? 'selected' : '' ?>>Weekly</option>
于 2013-08-04T16:27:15.677 回答
0

像这样更改代码,使其更具可读性和效率。

<?php
$income_select = "weekly"; //default value
$row = mysql_fetch_array($result);
$income_select = $row['income_select'];

echo $income_select; // echo so you can see the value and debug why it doesnt work because the code looks ok at first sight
?>

<select id="select-edit-1">
 <option value="daily" <?php if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
 <option value="weekly" <?php if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>

于 2013-08-04T16:31:11.993 回答
0

你应该试试看,

<select id="select-edit-1">
    <?php
       $row = mysql_fetch_array($result);
       $income_select = $row['income_select'];// will return daily or weekly
       $dailySel='';$weeklySel='';
       if ($income_select == 'daily') 
       {
          $dailySel='selected="selected"';
       }   
       else
          $weeklySel='selected="selected"';
    ?>
    <option value="daily" <?php echo $dailySel;?>Daily</option>
    <option value="weekly" <?php echo $weeklySel; ?>Weekly</option>
 </select> 
于 2013-08-04T16:31:54.310 回答