当从MySQL数据库使用下拉框时,显示一行中的多个值 [英] Show multiple values from a row when dropdown box is used from mysql database

查看:177
本文介绍了当从MySQL数据库使用下拉框时,显示一行中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在下拉列表中选择一个项目时,我试图在该行中显示一个特定项目. 为了澄清,可以说我有在下拉菜单中选择,并选择的时候,我想价格在页面上的其他字段中显示.

I'm trying to show a specific item in the row when an item in the dropdown list is selected. To clarify, lets say I have item1 chosen in the dropdown menu, and when item1 is chosen, I want price for item1 shown in another field on the page.

ps.我正在尝试制作连接到MYSQL数据库的库存和订购表.

ps. I'm trying to make an inventory and ordering form which is connected to an MYSQL database.

谢谢.

这是我的PHP代码.

    <?php

    function dropdown( $alcohol, array $options, $selected=null )
    {
        /*** begin the select ***/
        $dropdown = '<select name="'.$alcohol.'" id="'.$alcohol.'">'."\n";

        $selected = $selected;
        /*** loop over the options ***/
        foreach( $options as $key=>$option )
        {
            /*** assign a selected value ***/
            $select = $selected==$key ? ' selected' : null;

            /*** add each option to the dropdown ***/
            $dropdown .= '<option                                       

`value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
        }

    /*** close the select ***/
    $dropdown .= '</select>'."\n";

    /*** and return the completed dropdown ***/
    return $dropdown;
}
?>

<form>
 <?php
mysql_connect('localhost', 'root', '');
mysql_select_db('');
$sql = "SELECT alcohol FROM alcohol ";
$result = mysql_query($sql);
echo "<select name='alcohol'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>";
}
echo "</select>";
?> 
</form>

//下面的新东西//

<form  id="data" class="form_alcohol" role="form" method="post" action="connect.php">
            <INPUT TYPE = "Submit" Name = "Submit" VALUE = "Submit">
            <select size="1" name="alcohol">
                <option value="">--- Select Alcohol ---</option>
                <?php 
                   mysql_connect('localhost', 'root', '');
                    mysql_select_db('');
                    $sql = "SELECT alcohol FROM alcohol";
                    $result1 = mysql_query($sql);

                    while ($row = mysql_fetch_array($result1)) {
                    echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>";
                    }
                    $dropdown1 = empty($_POST['alcohol'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['alcohol']);
echo "</select>";

                ?>

<?php 
if(isset($_POST['Submit'])) {
mysql_connect('localhost', 'root', '');
mysql_select_db('');
$sql = "SElECT * FROM alcohol where '$dropdown1' = alcohol";
$result = mysql_query($sql) or die(mysql_error());
?>
<table>
<td> alcohol </td> <td> price </td> <td> amount in stock </td>

<?php

 while ($row = mysql_fetch_array($result)) {
 echo "<tr><td>".$row['alcohol']."</td><td>".$row['price']."</td><td>".$row['quantity_in_stock']."</td>";

}
}
?>

</table>

推荐答案

首先,使用mysqli.不建议使用Mysql_ *,并将在以后的版本中将其删除.

First of all, use mysqli. Mysql_* is deprecated and will be removed in a future version.

现在我们已经解决了这个问题,我将在原始下拉选择值中添加一个标记"字符以获取该值.选择一个通常不会出现在数据库中的Unicode字符,然后在值中回显该字符,因此它看起来像这样:

Now that we have that out of the way, I would add a "marker" character in the original dropdown select values in order to get the value. Pick a Unicode character that wouldn't normally be in your database, and echo that in the value so it looks like this:

<option value="$name@$price">$name</option>

然后在上面的代码中,在@字符处拆分值,然后选择其后的内容以获取辅助值.从长远来看,这应该可以为您节省数据库操作.

Then in the code above, split the value at the @ character and pick out what is after it in order to get the secondary value. That should save you a database operation in the long run.

祝你好运!

这篇关于当从MySQL数据库使用下拉框时,显示一行中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆