我有以下表格,带有“选择”标签:
<form action="upload.php" enctype="multipart/form-data" method="post">
<br>Filename: <input name="userfile" type="file">
<p>Customer:
<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>
由于数据库只有一行,只有一个客户,我希望消除组合框并自动选择唯一一个客户。我怎样才能做到这一点?我真的是一个新手,经过大量修改,我仍然没有找到解决方案。
感谢您的帮助。
垫
更新
感谢一些建议,我正在研究这个修改...仍然不起作用,但我认为是正确的方法,不是吗?
以下是代码:
<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>
答案 0 :(得分:3)
根据建议,我将其作为答案发布:
$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>';
}
值中的...理想情况下是表格中的某种主键。
答案 1 :(得分:0)
在调用mysql_query
之后,您应该调用mysql_num_rows()
来查找从SELECT语句返回的行数,如果为1,则继续而不显示select。
答案 2 :(得分:0)
您无法消除硬编码到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。