【问题标题】:Use of select tag to display database in textbox PHP使用选择标签在文本框 PHP 中显示数据库
【发布时间】:2015-08-11 09:38:05
【问题描述】:

我正在尝试使用下拉菜单将我的数据库值显示到文本框中。这是我做的,它正在显示。这里的问题是,当我在下拉列表中选择一个项目时,它会回到第一个选择或最后一个选择,我得到的解释是,我的循环正在选择字段中的所有项目,导致下拉菜单当我点击其他项目时回到第一个选择。当我选择其他选项时,您能帮我提供有关如何停止回到第一选择的代码吗?这是我的整个代码。我也使用函数。

home.php

<?php
session_start();
include('dbconnect.php');
include('functions.php');

if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");

while($row = mysql_fetch_array($result)){
    $price = $row['price'];
    $stocks = $row['stocks'];
}
}

?>

<html>
<body>
    <form method="POST" name="">
        <table align="center">
            <tr>
                <td>Choose here:</td>
                <td>

                    <select name = "brandname" onchange = "this.form.submit()">
                        <?php dropdown() ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Quantity:</td>
                <td><input type="text" name="qty" id="qty" value="" /></td>
            </tr>
            <tr>
                <td>Price:</td>
                <td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
            </tr>
            <tr>
                <td>Stocks:</td>
                <td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
            </tr>
            <tr>
                <td>Total:</td>
                <td><input type="text" name="total" id="total" disabled/></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
    </form>
    <div align = "center">
    hi' <?php echo $userRow['username']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
    </div>
</body>
</html>

functions.php

<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){

    echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";

   }
}

随意编辑整个代码。我是 php 的初学者,正在学习我的方法。谢谢

【问题讨论】:

  • 你的循环中需要 selected='selected' 吗?只是意味着每一个都会被选中
  • 是..如果我删除选择标签..在我选择任何选项后,下拉菜单上的第一个选择总是被选中..但是如果我包含选择标签..最后一个选择在我选择其他选项后,总是会选择下拉菜单的 .. 是的.. 每个选项都会被选中,这正是我的问题.. :D

标签: php html sql


【解决方案1】:

如果需要选择多个,可以添加多个选项

<select name="brandname" multiple>
   <option value="Select">Select</option>
  <?php
  do {  
  ?>
 <option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
 <?php
 } while ($row = mysql_fetch_assoc($all));
 ?>
</select>

【讨论】:

  • 谢谢.. 嗯.. 我应该把这个放在哪里。?在functions.php或home.php ..?我马上试试..
  • 但就像他们在演示中所说的那样,如果您选择多个复选框,则可以选择复选框
  • 是的..我仔细阅读了..哈哈哈..谢谢伙计..但我得到了这个未定义的变量“行”..对不起..这里完全是菜鸟..:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2017-08-21
  • 2019-05-15
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多