使用 PHP 从 MySQL DB 更新 HTML 组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17610157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:10:31  来源:igfitidea点击:

Updating an HTML combo box from MySQL DB using PHP

phphtmlmysqlforms

提问by vjrngn

need some help here. It's a very simple web app i'm developing but just needed some help with something.

这里需要一些帮助。这是我正在开发的一个非常简单的网络应用程序,但只是需要一些帮助。

Here's what's setup. I have a html form with one combo box. All I need is to update this combo box with the entries from a mysql table named 'supplier'. The input to this table 'supplier' is via another form on my website which i've already setup. I need help in auto updating this combo box from the table 'supplier'. Please let me know the php code for it. I've included my code as well. Thanks in advance! I have included the html form as well.

这是设置的内容。我有一个带有一个组合框的 html 表单。我所需要的只是使用名为“供应商”的 mysql 表中的条目更新此组合框。这个表“供应商”的输入是通过我网站上的另一个表格,我已经设置了。我需要帮助从表“供应商”自动更新此组合框。请让我知道它的php代码。我也包含了我的代码。提前致谢!我也包含了 html 表单。

Here's what's happening after making the edits you guys suggested :)

这是进行你们建议的编辑后发生的事情:)

采纳答案by Hitesh Siddhapura

replace your code

替换你的代码

 $result = mysql_query("SELECT supplier FROM supplier"); 
            while($row = mysql_fetch_array($result)) 
                    { 
                    /*echo '<form action="">';*/ 
                    echo "<select name='supplier'>"; 
            echo "<option value = '$row[supplier]'>""</option>"; 
                    echo "</select>"; 

with

 $result = mysql_query("SELECT supplier FROM supplier"); 
 echo "<select name='supplier'>"; 
 while($row = mysql_fetch_assoc($result)) 
 { 
    echo "<option value = '".$row[supplier]."'>".$row[supplier]."</option>"; 
 }
 echo "</select>"; 

回答by Micha El

So,

所以,

what is the problem? Is your script not working or do you want a active Combobox on one screen to be updated, when on another screen a new entry for supplier is entered?

问题是什么?当在另一个屏幕上输入供应商的新条目时,您的脚本是否不起作用,或者您是否希望更新一个屏幕上的活动组合框?

Ok, some hints:

好的,一些提示:

  1. The "select"-Tag should be placed outside the loop.
  2. Put the column name in "
  3. Add a display-Value for the options
  1. “select”-Tag 应该放在循环之外。
  2. 将列名放在“
  3. 为选项添加显示值

So, without checking it:

所以,不检查它:

/*echo '<form action="">';*/
echo "<select name='supplier'>";    
while($row = mysql_fetch_array($result)) 
{ 
    echo "<option value = '".$row["supplier"]."'>".$row["supplier"]."</option>"; 
}                        
echo "</select>"; 

回答by fantasitcalbeastly

Your problem is here:

你的问题在这里:

            $result = mysql_query("SELECT supplier FROM supplier"); 
            while($row = mysql_fetch_array($result)) 
                    { 
                    /*echo '<form action="">';*/ 
                    echo "<select name='supplier'>"; 
            echo "<option value = '$row[supplier]'>""</option>"; 
                    echo "</select>"; 

You're creating the drop down box (the Select) inside of the mysql data loop. As @Hitesh has explained. You need to create this outside of the loop and only echo out the data results within. For example:

您正在Selectmysql 数据循环内创建下拉框 (the )。正如@Hitesh 所解释的那样。您需要在循环之外创建它,并且只回显其中的数据结果。例如:

$result = mysql_query("SELECT supplier FROM supplier");
echo "<select name='supplier'>";
while($row = mysql_fetch_array($result))
{
    echo "<option value=".$row['supplier'].">".$row['supplier']."</option>";
}
echo "</select>";

This will output your drop down box, with all your supplier names as the value and the displayed text options.

这将输出您的下拉框,其中包含所有供应商名称作为值和显示的文本选项。

If you attempted to do the following:

如果您尝试执行以下操作:

            $result = mysql_query("SELECT supplier FROM supplier"); 
            while($row = mysql_fetch_array($result)) 
                    { 
                    /*echo '<form action="">';*/ 
                    echo "<select name='supplier'>"; 
            echo "<option value = '$row[supplier]'>""</option>"; 
                    echo "</select>"; 
                    }

You would just end up with as many drop down boxes as you had suppliers in your database. This is because you're creating a new Selectbox for each record found.

您最终会得到与数据库中的供应商一样多的下拉框。这是因为您要Select为找到的每条记录创建一个新框。

Also, without specifying the second $row['supplier']between the optiontags, you'd just end up with a blank (empty) drop down box.

此外,如果不指定标签$row['supplier']之间的第二个option,您只会得到一个空白(空)下拉框。

Hope this helps.

希望这可以帮助。

回答by Neeraj Singh

Here is complete code:

这是完整的代码:

PHP Code:

PHP代码:

<?php
// select box open tag
$selectBoxOpen =  "<select name='supplier'>"; 
// select box close tag
$selectBoxClose =  "</select>";
// select box option tag
$selectBoxOption = ''; 

// connect mysql server
$con = mysql_connect("localhost","user","pass"); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 

// select database
mysql_select_db("rtgs", $con); 
// fire mysql query
$result = mysql_query("SELECT supplier FROM supplier");
// play with return result array 
while($row = mysql_fetch_array($result)){   
    $selectBoxOption .="<option value = '".$row['supplier']."'>".$row['supplier']."</option>"; 
}
// create select box tag with mysql result
$selectBox =  $selectBoxOpen.$selectBoxOption.$selectBoxClose;   
?>

HTML Code:

HTML代码:

<form action="ntxn.php" method="post">

<table>
    <tr>
        <td>Supplier Name:</td>
        <td> 
            <?php echo $selectBox;?>
        </td>
    </tr>

    <tr>
        <td>Bill No. :</td>
        <td><input type ="text" name="billno"/></td>
    </tr>

    <tr>
        <td>Bill Date : </td>
        <td><input type="date" name="billdate"/></td>
    </tr>

    <tr>
        <td>Bill Amount : </td>
        <td><input type="text" name="billamt"/></td>
    </tr>

    <tr>
        <td>NEFT / RTGS No. :</td>
        <td><input type="text" name="rtgs"/></td>
    </tr>

    <tr>
        <td><input type="submit" name="Save"/></td>
    </tr>
</table>

</form>