如何从表单中消除组合框


How to eliminate a combo box from a form

我有一个带有" select "标签的表单:

  <form action="upload.php" enctype="multipart/form-data" method="post">
  <br>Filename:&nbsp;<input name="userfile" type="file">
  <p>Customer:&nbsp;      

  <select name="customer">
  <?php
include 'data.dat';
include 'error.inc';
// Connect to the database
if (!($connection = @ mysql_connect ($hostName,
                                    $username,
                                    $password)))
die ("Could not connect to database");
// Select the database
if (!mysql_select_db ($databaseName, $connection))
    showerror();
// Display all the available customers in order
$query = "SELECT customer FROM custlist order by id";
if (!($result = @ mysql_query ($query, $connection)))
    showerror();
$i = 0;
// Display each customer in a drop down menu. 
while ($row = @ mysql_fetch_array ($result))
{
    $i++;
    if ($i == 1)
        echo "<option value=" . $i . " selected>" . $row['customer'] . "'n";
    else
        echo "<option value=" . $i . ">" . $row['customer'] . "'n";
  //
}
  //

  ?>
  </select>
  <input type="submit" value="Upload file">
  </form>

由于数据库只有1行,只有一个客户,我希望消除组合框并自动选择唯一的一个客户。我该怎么做呢?我真的是一个新手,经过很多修改,我仍然没有找到解决方案。

谢谢你的帮助。

感谢一些建议,我正在努力修改…还是不行,但我认为这是正确的方法,不是吗?

代码如下:

  <select name="customer">
  <?php
include 'data.dat';
include 'error.inc';
// Connect to the database
if (!($connection = @ mysql_connect ($hostName,
                                    $username,
                                    $password)))
die ("Could not connect to database");
// Select the log database
if (!mysql_select_db ($databaseName, $connection))
    showerror();
// Display all the available customers in order
$query = "SELECT customer FROM curlist order by id";
  $result = mysql_query($query) or die(mysql_error());

  $row = mysql_fetch_array($result) or die(mysql_error());
  echo  $row['customer'];

  ?>
  </select>

根据建议,我把我的尝试作为一个答案:

$count = mysql_num_rows($result);
if($count == 1) {
    $row = mysql_fetch_assoc($result);
    echo '<input type="text" value="' . htmlentities($row['customer']) . '" readonly />';
    echo '<input type="hidden" name="customer" value="..." />';
} else {
    echo '<select name="customer">';
    while($row = mysql_fetch_assoc($result)) {
        echo '<option value="...">' . htmlentities($row['customer']) . '</option>';
    }
    echo '</select>';
}

在调用mysql_query之后,您应该调用mysql_num_rows()来查找从SELECT语句返回的行数,如果是1,则继续执行,而不显示SELECT

您无法消除硬编码到HTML结构中的组合框。

将此HTML合并到您的代码中,使用如下代码:

// Get number of rows returned by query
$count = mysql_num_rows($result);
// Start iteration counter
$i = 1; # start at one to marry up with $count
// Iterate through array of mySQL rows
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
    if ($count == 1) {  # Evals true if 1 customer is returned
        $html = "<input type='"textbox'" value='"". $row['customer'] . "'" />";
    // If multiple customers are returned, 
    // but it's the first iteration through the loop. Create Select box.
    } elseif ($i == 1 && $count != 1) { 
        $html .= "<select name='"customer'">'n";
        $html .= "<option value='"" . $row['id'] . "'">" . $row['customer'] . "</option>'n";
    // If this is the last row, close the select box.
    } elseif ($i == $count) {
        // 
        $html .= "<option value='"" . $row['id'] . "'">" . $row['customer'] . "</option>'n";
        $html .= "</select>'n";
    // If none of the above, create new option
    } else {
        $html .= "<option value='"" . $row['id'] . "'">" . $row['customer'] . "</option>'n";
    }
    $i++;
}
echo $html;

编辑:从下面的评论中,将<option>值属性更改为客户行ID。